awk '/start_pattern/,/stop_pattern/' file.txt

Display a block of text with AWK

I find this terribly useful for grepping through a file, looking for just a block of text. There's "grep -A # pattern file.txt" to see a specific number of lines following your pattern, but what if you want to see the whole block? Say, the output of "dmidecode" (as root): dmidecode | awk '/Battery/,/^$/' Will show me everything following the battery block up to the next block of text. Again, I find this extremely useful when I want to see whole blocks of text based on a pattern, and I don't care to see the rest of the data in output. This could be used against the '/etc/securetty/user' file on Unix to find the block of a specific user. It could be used against VirtualHosts or Directories on Apache to find specific definitions. The scenarios go on for any text formatted in a block fashion. Very handy.

By: atoponce
2009-03-28 14:28:59

2 Alternatives + Submit Alt

What Others Think

Very nice. dmidecode | awk '/Core/,/^$/'
Williebee · 477 weeks and 1 day ago
Awesome! Awk is such a powerful tool and I always wish I knew it better.
philiph · 477 weeks and 1 day ago
Thanks a lot
megerdin · 477 weeks ago
Nice! I like using this for xml files.
tajohnson · 471 weeks and 2 days ago
because your '^$' is included in the pattern it'll also be output. You can prevent the empty line being output with the following: awk '/foo/,/^$/{if ($0 > 0) print}' Also, if the data is structured in a consistant way you can set RS to the particular record, ie: awk '{RS="Cell"}/MY_AP/' which will output the particular record for the ESSID "MY_AP".
khayyam · 269 weeks and 2 days ago
hmmmm ... the last command was chomped, I assume as I used a subshell ... anyhow it should read (without subshell): iwlist wlan0 scan |awk '{RS="Cell"}/MY_AP/'
khayyam · 269 weeks and 2 days ago
Does not work
iamvivek · 164 weeks and 4 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Similar Commands

extract XML RSS etc by tags such as or <code> or <description></a></strong> <br/> <span class="text-muted"><a class="text-muted" href="/commands/by/c3w">c3w</a> <span class="pull-right">0 </span> </span> </p> <p> <strong><a href="/commands/view/4382/display-embeded-comments-for-every-opt-usefull-for-auto-documenting-your-script">display embeded comments for every --opt, usefull for auto documenting your script</a></strong> <br/> <span class="text-muted"><a class="text-muted" href="/commands/by/syladmin">syladmin</a> <span class="pull-right">0 </span> </span> </p> <p> <strong><a href="/commands/view/5087/multi-line-grep">Multi-line grep</a></strong> <br/> <span class="text-muted"><a class="text-muted" href="/commands/by/hfs">hfs</a> <span class="pull-right">7 </span> </span> </p> <p> <strong><a href="/commands/view/11905/display-the-hover-text-of-the-most-recent-xkcd">display the hover text of the most recent xkcd</a></strong> <br/> <span class="text-muted"><a class="text-muted" href="/commands/by/putnamhill">putnamhill</a> <span class="pull-right">0 </span> </span> </p> <p> <strong><a href="/commands/view/2183/how-to-find-the-block-size">How to Find the Block Size</a></strong> <br/> <span class="text-muted"><a class="text-muted" href="/commands/by/rez0r">rez0r</a> <span class="pull-right">0 </span> </span> </p> <p> <strong><a href="/commands/view/9203/text-message-on-wallpaper">Text message on wallpaper</a></strong> <br/> <span class="text-muted"><a class="text-muted" href="/commands/by/totti">totti</a> <span class="pull-right">1 </span> </span> </p> </div> <hr> <div class="box-copy"> <h3 class="righthead">Stay in the loop…</h3> <div class="toggleable"> <dl> <dt>Follow the Tweets.</dt> <dd> <a href=""><img class="link-img" src="/images/twitter.jpg" /></a> <p>Every new command is wrapped in a tweet and posted to <a href="">Twitter</a>. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.</p> <p> » <a style="font-weight:bold;" href=""></a><br/> » <a style="font-weight:bold;" href=""></a><br/> » <a style="font-weight:bold;" href=""></a> </p> </dd> <dt>Subscribe to the feeds.</dt> <dd> <a style="font-weight:bold;" href=""><img class="link-img" src="/images/rss.gif" /></a> <p>Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):</p> <p>Subscribe to the feed for:</p> <ul id="feed-links"> <li>» <a style="font-weight:bold;" href="">all commands</a></li> <li>» <a style="font-weight:bold;" href="/feed/threeup">commands with 3 up-votes</a> (commandlinefu3)</li> <li>» <a style="font-weight:bold;" href="/feed/tenup">commands with 10 up-votes</a> (commandlinefu10)</li> </ul> </dd> </dl> </div> </div> <!-- <div class="box-copy"> <a class="visibility-toggle" id="box-copy-tags">Hide</a> <h3>Top Tags</h3> <div class="toggleable"><br> <a href="/commands/tagged/112/awk">awk</a>, <a href="/commands/tagged/34/bash">bash</a></li>, <a href="/commands/tagged/235/curl">curl</a>, <a href="/commands/tagged/123/find">find</a>, <a href="/commands/tagged/163/grep">grep</a>, <a href="/commands/tagged/64/linux">Linux</a>, <a href="/commands/tagged/110/sed">sed</a></li> </ul> </div> </div> <div class="box-copy"> <a class="visibility-toggle" id="box-copy-functions">Hide</a> <h3>Functions</h3> <div class="toggleable"> </div> </div> --> </div> </div> </div> <footer class="bs-footer" role="contentinfo" style="background-color:#F5F5F5;"> <div class="container"> <p style="padding-top:20px;"> © 2018 <a href="" target="_blank">Dan's Tools</a> | <a href="/site/faq">FAQs</a> | <a href="/site/api">API</a> | <a href="/site/widget">Widget</a> </p> </div> </footer> <!-- <div id="wrapper"> <div id="right-block"> </div> <div id="header"> <h4><a href="/commands/browse" title=" - the greatest commands on the web"><img src="/images/header-logo.jpg" alt="" /></a></h4> <div id="nav"> <strong><a href="/commands/random" title="Lucky dip">Random</a></strong> | <strong><a href="/commands/browse/last-week/sort-by-votes" title="Piping host commands from the last 7 days">Hot this week</a></strong> | <strong><a href="/commands/browse/sort-by-votes" title="See the all-time greatest commands">All-time greats</a></strong> • <strong><a href="/site/faq"><abbr title="Frequently asked questions">FAQ</abbr></a></strong> | <strong><a href="/site/api"><abbr title="API documentation">API</abbr></a></strong> | <strong><a href="/site/widget"><abbr title="Javascript widget">Widget</abbr></a></strong> </div> <div id="search"> <form id="search-form" action="/search/handler" method="post" > <div id="search-container"> <img id="loading" src="/images/tag-loader.gif" style="display:none;float:right;margin: 5px 5px 0 0;" /> <input type="text" id="search-query" class="inactive" name="q" autocomplete="off" value="" placeholder="type here to grep the archive..." /> </div> </form> <div style="display:none" id="search-options"></div> </div> </div> <div id="left-block"> </div> <div id="footer"> <div class="advertising"> </div> </div> </div> --> <script src="//"></script> <script> $.noConflict(); if (typeof Prototype !== "undefined" && Prototype.BrowserFeatures.ElementExtensions) { var disablePrototypeJS = function (method, pluginsToDisable) { var handler = function (event) {[method] = undefined; setTimeout(function () { delete[method]; }, 0); }; pluginsToDisable.each(function (plugin) { jQuery(window).on(method + '.bs.' + plugin, handler); }); }, pluginsToDisable = ['collapse', 'dropdown', 'modal', 'tooltip', 'popover', 'tab','collect']; disablePrototypeJS('show', pluginsToDisable); disablePrototypeJS('hide', pluginsToDisable); } </script> <script> jQuery( document ).ready(function() { hljs.initHighlightingOnLoad(); hljs.configure({useBR: true}); jQuery('span.code').each(function(i, block) { hljs.highlightBlock(block); }); }); </script> <script src="//"></script> </body> </html>