Display a block of text with AWK

sed -n /start_pattern/,/stop_pattern/p file.txt

7
By: pipping
2009-10-24 16:35:39
sed

3 Alternatives + Submit Alt

  • 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.


    86
    awk '/start_pattern/,/stop_pattern/' file.txt
    atoponce · 2009-03-28 14:28:59 7
  • -n reads input, line by line, in a loop sending to $_ Equivalent to while () { mycode } -e execute the following quoted string (i.e. do the following on the same line as the perl command) the elipses .. operator behaves like a range, remembering the state from line to line.


    2
    perl -ne 'print if /start_pattern/../stop_pattern/' file.txt
    SunKing2 · 2018-07-24 03:23:12 1
  • By using vim, you can also filter content on stdout, using vim's extra power, like search pattern offset! No more awk of course, sorry. details : -e ex mode -s silent -c 'ex command' : global + start and end pattern + offset print (p) -cq : quit Show Sample Output


    0
    vim -e -s -c 'g/start_pattern/+1,/stop_pattern/-1 p' -cq file.txt
    syladmin · 2009-08-26 10:22:27 0

What Others Think

I've found this very useful in the past.
jgc · 525 weeks 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?

commandlinefu.com 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> </p> <p style="overflow:hidden;white-space:nowrap;text-overflow:ellipsis"> <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> </p> <p style="overflow:hidden;white-space:nowrap;text-overflow:ellipsis"> <strong><a href="/commands/view/5087/multi-line-grep">Multi-line grep</a></strong> </p> <p style="overflow:hidden;white-space:nowrap;text-overflow:ellipsis"> <strong><a href="/commands/view/2183/how-to-find-the-block-size">How to Find the Block Size</a></strong> </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="http://twitter.com/commandlinefu"><img class="link-img" src="/images/twitter.jpg" /></a> <p>Every new command is wrapped in a tweet and posted to <a href="http://twitter.com">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="http://twitter.com/commandlinefu">http://twitter.com/commandlinefu</a><br/> » <a style="font-weight:bold;" href="http://twitter.com/commandlinefu3">http://twitter.com/commandlinefu3</a><br/> » <a style="font-weight:bold;" href="http://twitter.com/commandlinefu10">http://twitter.com/commandlinefu10</a> </p> </dd> <dt>Subscribe to the feeds.</dt> <dd> <a style="font-weight:bold;" href="http://feeds2.feedburner.com/Command-line-fu"><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="http://feeds2.feedburner.com/Command-line-fu">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;">commandlinefu.com © 2018 <a href="https://www.danstools.com" target="_blank">Dan's Tools</a> | <a href="/site/faq">FAQs</a> | <a href="/site/api">API</a> | <a href="/site/widget">Widget</a> | <a href="/site/privacy">Privacy</a></p> </div> </footer> <!-- <div id="wrapper"> <div id="right-block"> </div> <div id="header"> <h4><a href="/commands/browse" title="commandlinefu.com - the greatest commands on the web"><img src="/images/header-logo.jpg" alt="commandlinefu.com" /></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="//code.jquery.com/jquery-1.11.0.min.js"></script> <script> $.noConflict(); if (typeof Prototype !== "undefined" && Prototype.BrowserFeatures.ElementExtensions) { var disablePrototypeJS = function (method, pluginsToDisable) { var handler = function (event) { event.target[method] = undefined; setTimeout(function () { delete event.target[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); }); if (1==0 && window.canRunAds === true) { jQuery('#blocker').hide(); } else { jQuery('#blocker').show(); } }); </script> <script src="//netdna.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script> <script src="/js/ads.js"></script> <script type='text/javascript' style='display:none;' async> __ez.queue.addFile('edmonton.php', '/detroitchicago/edmonton.webp?a=a&cb=0&shcb=32', true, [], true, false, false, false); __ez.queue.addFile('jellyfish.php', '/porpoiseant/jellyfish.webp?a=a&cb=0&shcb=32', false, [], true, false, false, false); </script> <script>var _audins_dom="commandlinefu_com",_audins_did=114522;__ez.queue.addDelayFunc("audins.js","__ez.script.add", "//go.ezoic.net/detroitchicago/audins.js?cb=188-0");</script><noscript><div style="display:none;"><img src="//pixel.quantserve.com/pixel/p-31iz6hfFutd16.gif?labels=Domain.commandlinefu_com,DomainId.114522" border="0" height="1" width="1" alt="Quantcast"/></div></noscript><noscript><img src="https://sb.scorecardresearch.com/p?c1=2&c2=20015427&cv=2.0&cj=1"/></noscript> <script type='text/javascript'>(function(){function f(b,d){for(var c=0;c<d.length;c++){var e=d[c];if(0==e.complete||"undefined"!=typeof e.readyState&&4>e.readyState){var k=e.getAttribute("src")||e.currentSrc;"undefined"!=typeof e.readyState&&0==e.readyState?e.addEventListener("loadstart",function(g){var l=g.getAttribute("src")||g.currentSrc;h(g,l)}):(k=e.getAttribute("src")||e.currentSrc,h(e,k));e.addEventListener("load",function(g){var l=g.currentTarget.getAttribute("src")||g.srcElement.currentSrc;m(g,l)});e.addEventListener("loadeddata", function(g){var l=g.currentTarget.getAttribute("src")||g.srcElement.currentSrc;m(g,l)});e.addEventListener("error",function(g){var l=g.currentTarget.getAttribute("src")||g.srcElement.currentSrc;m(g,l)})}}}function a(b){for(var d=0;d<document.styleSheets.length;d++)if(document.styleSheets[d].href==b)return!0;return!1}f("img",document.querySelectorAll("img"));f("video",document.querySelectorAll("video"));f("audio",document.querySelectorAll("audio"));(function(b){for(var d=0;d<b.length;d++){var c=b[d]; if("stylesheet"==c.getAttribute("rel")&&null!=c.getAttribute("href")&&a(c.getAttribute("href"))){h(c,c.getAttribute("href"));var e=document.createElement("img");e.onerror=function(k){m(c,k.path[0].currentSrc)};e.src=c.getAttribute("href")}}})(document.querySelectorAll("link"))})();</script><script type="text/javascript" onload="__ezcl.handle(true);" async src="/utilcave_com/inc/ezcl.webp?cb=4"></script></body> </html>