Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>another awk solution </p> <p>if the block you are gonna extract is always at the top of the file, and there will be only 1 or 0 block in your output:</p> <pre><code>awk '/\[Tracer\]/{print;f=1;next;} {if(NR==2){if(f &amp;&amp; $0~/interest/){print;next;} else exit;} if(f &amp;&amp; $0) print; else exit;}' file </code></pre> <p>if you want to catch more blocks:</p> <pre><code>awk '/\[Tracer\]/{h=$0;f=1;l=NR;next;} {if(NR==l+1){if(f &amp;&amp; $0~/interest/){print h;print;} else {f=0;h=""; }next;} if(f) if($0)print;else f=0; }' file </code></pre> <p><strong>test on both above</strong></p> <pre><code> #input file, in this case, 1st and 3rd blocks should be in your output (for solution 2) kent$ cat file 2012-01-12 22:20:21,638 INFO [Tracer] interesting ...some number of additional lines... xxx yyy zzz ...other stuff... 2012-01-12 22:20:21,638 INFO [Tracer] NOT-wanted-NOT ...some number of additional lines... xxx#### yyy#### zzz#### ...other #### stuff... 2012-01-12 22:20:21,638 INFO [Tracer] interest ...some number of additional lines... xxxWANTWANTWANT yyy.WANTWANTWANT zzzoWANTWANTWANT blahblah #solution one: kent$ awk '/\[Tracer\]/{print;f=1;next;} {if(NR==2){if(f &amp;&amp; $0~/interest/){print;next;} else exit;} if(f &amp;&amp; $0) print; else exit;}' file 2012-01-12 22:20:21,638 INFO [Tracer] interesting ...some number of additional lines... xxx yyy zzz #solution two: kent$ awk '/\[Tracer\]/{h=$0;f=1;l=NR;next;} {if(NR==l+1){if(f &amp;&amp; $0~/interest/){print h;print;} else {f=0;h=""; }next;} if(f) if($0)print;else f=0; }' file 2012-01-12 22:20:21,638 INFO [Tracer] interesting ...some number of additional lines... xxx yyy zzz 2012-01-12 22:20:21,638 INFO [Tracer] interest ...some number of additional lines... xxxWANTWANTWANT yyy.WANTWANTWANT zzzoWANTWANTWANT </code></pre> <p>hope it helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload