<div>I used whatever was on my Fedora 13 box:</div><div><br></div><div>[sean@bob ~]$ awk --version</div><div>GNU Awk 3.1.8</div><div>[sean@bob ~]$ sed --version</div><div>GNU sed version 4.2.1</div><div><br></div><div>The difference gets much bigger if you use a more complex regexp.</div>
<div><br></div><div><div>[sean@bob tmp]$ time awk &#39;/.*output.*start.*/,/.*output.*end.*/&#39; &lt; infile &gt; /dev/null</div><div><br></div><div>real    0m0.450s</div><div>user    0m0.393s</div><div>sys     0m0.010s</div>
<div>[sean@bob tmp]$ time  sed -n &#39;/.*output.*start.*/,/.*output.*end.*/p&#39; &lt; infile &gt; /dev/null</div><div><br></div><div>real    0m1.726s</div><div>user    0m1.495s</div><div>sys     0m0.017s</div></div><div>
<br></div><div>Awk didn&#39;t seem to blink an eye. Strangely enough, since the beginning and ending .*&#39;s are completely superfluous, they seem to throw sed for a loop, even if the middle .* is replaced with a space.</div>
<div><br></div><div>Sean</div><br><div class="gmail_quote">On Wed, Nov 10, 2010 at 12:37 PM, Gilles Detillieux <span dir="ltr">&lt;<a href="mailto:grdetil@scrc.umanitoba.ca">grdetil@scrc.umanitoba.ca</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Interesting!  Which version of awk did you test?  I have to admit I<br>
haven&#39;t looked into awk performance in quite some time.  My early<br>
experience, on older Unix systems (pre-Linux), confirmed what I had read<br>
about awk being pretty slow.  But I seem to recall that even on older<br>
Linux systems, gawk wasn&#39;t exactly speedy then either.  I imagine the<br>
GNU awk developers must have remedied that since, though, if that is<br>
indeed what you were testing.<br>
<br>
Searching online for discussions on awk performance found one from 2002<br>
suggesting gawk was much faster than nawk, and another from this past<br>
August that suggested the opposite.  Perhaps the developers of the two<br>
have been leap-frogging each other with optimizations to their code?<br>
<div class="im"><br>
On 11/10/2010 11:56 AM, Sean Walberg wrote:<br>
&gt; Adam and I were having an offline discussion, and some testing shows<br>
&gt; that AWK outperforms SED by a slight margin:<br>
&gt;<br>
&gt; [sean@bob tmp]$ W=/usr/share/dict/words<br>
&gt; [sean@bob tmp]$ (tail -1000 $W; echo output start; cat $W; echo output<br>
&gt; end; head -1000 $W) &gt; infile<br>
&gt; [sean@bob tmp]$ wc -l infile<br>
&gt; 481831 infile<br>
&gt; [sean@bob tmp]$ time awk &#39;/output start/,/output end/&#39; &lt; infile &gt; /dev/null<br>
&gt;<br>
&gt; real    0m0.411s<br>
&gt; user    0m0.393s<br>
&gt; sys     0m0.016s<br>
&gt; [sean@bob tmp]$ time  sed -n &#39;/output start/,/output end/p&#39; &lt; infile &gt;<br>
&gt; /dev/null<br>
&gt;<br>
&gt; real    0m0.678s<br>
&gt; user    0m0.631s<br>
&gt; sys     0m0.029s<br>
&gt;<br>
&gt; I ran it a bunch more times and the results were similar.  YMMV,<br>
&gt; benchmarks are lies, etc.<br>
&gt;<br>
&gt; Sean<br>
&gt;<br>
&gt; On Wed, Nov 10, 2010 at 11:32 AM, Gilles Detillieux<br>
</div><div><div></div><div class="h5">&gt; &lt;<a href="mailto:grdetil@scrc.umanitoba.ca">grdetil@scrc.umanitoba.ca</a> &lt;mailto:<a href="mailto:grdetil@scrc.umanitoba.ca">grdetil@scrc.umanitoba.ca</a>&gt;&gt; wrote:<br>

&gt;<br>
&gt;     I may have misinterpreted the question before.  If you want the &quot;output<br>
&gt;     start&quot; and &quot;output end&quot; marker lines in the output (which I guess your<br>
&gt;     grep pipeline would do), then Adam&#39;s sed script will do that.  Mine,<br>
&gt;     using the &quot;d&quot; commands, will output only the data in between.  The<br>
&gt;     shortest awk script to do the same would be:<br>
&gt;<br>
&gt;     awk &#39;/output start/{s=1};s==1;/output end/{s=0};&#39;<br>
&gt;<br>
&gt;     or<br>
&gt;<br>
&gt;     awk &#39;/output end/{s=0};s==1;/output start/{s=1};&#39;<br>
&gt;<br>
&gt;     The first is a simplification of Adam&#39;s, which outputs the output marker<br>
&gt;     lines, while the second, using the same statements in the opposite<br>
&gt;     order, suppresses the markers.  Of perl, awk and sed, I suspect sed is<br>
&gt;     the most lightweight, and probably the quickest, unless perl can<br>
&gt;     outperform sed on larger files.  awk has a reputation for being pretty<br>
&gt;     slow.  I tend to favour sed unless awk or perl makes the job a lot<br>
&gt;     easier.<br>
&gt;<br>
&gt;     Gilles<br>
&gt;<br>
&gt;     On 11/10/2010 11:13 AM, Adam Thompson wrote:<br>
&gt;      &gt; The AWK version is functionally identical, and not very much<br>
&gt;     shorter, or<br>
&gt;      &gt; any more elegant:<br>
&gt;      &gt;<br>
&gt;      &gt;     awk ‘/output start/ {s=1};{if (s==1) print $0};/output end/<br>
&gt;     {s=0}’<br>
&gt;      &gt;<br>
&gt;      &gt; (the perl version can generally be made that small, too.)<br>
&gt;      &gt;<br>
&gt;      &gt;<br>
&gt;      &gt;<br>
&gt;      &gt; I would instead suggest sed(1), since this is precisely what it’s<br>
&gt;      &gt; designed for:<br>
&gt;      &gt;<br>
&gt;      &gt;     sed –n ‘/output start/,/output end/p’ &lt; infile<br>
&gt;      &gt;<br>
&gt;      &gt;<br>
&gt;      &gt;<br>
&gt;      &gt; -Adam<br>
&gt;      &gt;<br>
&gt;      &gt;<br>
&gt;      &gt;<br>
&gt;      &gt;<br>
&gt;      &gt;<br>
&gt;      &gt; *From:* <a href="mailto:roundtable-bounces@muug.mb.ca">roundtable-bounces@muug.mb.ca</a><br>
&gt;     &lt;mailto:<a href="mailto:roundtable-bounces@muug.mb.ca">roundtable-bounces@muug.mb.ca</a>&gt;<br>
&gt;      &gt; [mailto:<a href="mailto:roundtable-bounces@muug.mb.ca">roundtable-bounces@muug.mb.ca</a><br>
&gt;     &lt;mailto:<a href="mailto:roundtable-bounces@muug.mb.ca">roundtable-bounces@muug.mb.ca</a>&gt;] *On Behalf Of *Sean Walberg<br>
&gt;      &gt; *Sent:* Wednesday, November 10, 2010 10:56<br>
&gt;      &gt; *To:* Continuation of Round Table discussion<br>
&gt;      &gt; *Subject:* Re: [RndTbl] Command line challenge: trim garbage from<br>
&gt;     start<br>
&gt;      &gt; and end of a file.<br>
&gt;      &gt;<br>
&gt;      &gt;<br>
&gt;      &gt;<br>
&gt;      &gt; OTTOMH:<br>
&gt;      &gt;<br>
&gt;      &gt;<br>
&gt;      &gt;<br>
&gt;      &gt; perl -n -e &#39;BEGIN {$state = 0} $state = 1 if ($state == 0 and /output<br>
&gt;      &gt; start/); $state = 2 if ($state == 1 and /output end/)  ; print if<br>
&gt;      &gt; ($state == 1)&#39; &lt; infile &gt; outfile<br>
&gt;      &gt;<br>
&gt;      &gt; I&#39;ll bet there&#39;s a shorter AWK version though.<br>
&gt;      &gt;<br>
&gt;      &gt;<br>
&gt;      &gt;<br>
&gt;      &gt; Sean<br>
&gt;      &gt;<br>
&gt;      &gt;<br>
&gt;      &gt;<br>
&gt;      &gt; On Wed, Nov 10, 2010 at 10:51 AM, John Lange &lt;<a href="mailto:john@johnlange.ca">john@johnlange.ca</a><br>
&gt;     &lt;mailto:<a href="mailto:john@johnlange.ca">john@johnlange.ca</a>&gt;<br>
</div></div><div class="im">&gt;      &gt; &lt;mailto:<a href="mailto:john@johnlange.ca">john@johnlange.ca</a> &lt;mailto:<a href="mailto:john@johnlange.ca">john@johnlange.ca</a>&gt;&gt;&gt; wrote:<br>
&gt;      &gt;<br>
&gt;      &gt; I have files with the following structure:<br>
&gt;      &gt;<br>
&gt;      &gt; garbage<br>
&gt;      &gt; garbage<br>
&gt;      &gt; garbage<br>
&gt;      &gt; output start<br>
&gt;      &gt; .. good data<br>
&gt;      &gt; .. good data<br>
&gt;      &gt; .. good data<br>
&gt;      &gt; .. good data<br>
&gt;      &gt; output end<br>
&gt;      &gt; garbage<br>
&gt;      &gt; garbage<br>
&gt;      &gt; garbage<br>
&gt;      &gt;<br>
&gt;      &gt; How can I extract the good data from the file trimming the garbage<br>
&gt;      &gt; from the beginning and end?<br>
&gt;      &gt;<br>
&gt;      &gt; The following works just fine but it&#39;s dirty because I don&#39;t like the<br>
&gt;      &gt; fact that I have to pick an arbitrarily large number for the &quot;before&quot;<br>
&gt;      &gt; and &quot;after&quot; values.<br>
&gt;      &gt;<br>
&gt;      &gt; grep -A 999999 &quot;output start&quot; &lt;infile&gt; | grep -B 999999 &quot;output<br>
&gt;     end&quot; &gt;<br>
&gt;      &gt; newfile<br>
&gt;      &gt;<br>
&gt;      &gt; Can anyone come up with something more elegant?<br>
&gt;      &gt;<br>
&gt;      &gt; --<br>
&gt;      &gt; John Lange<br>
</div>&gt;      &gt; <a href="http://www.johnlange.ca" target="_blank">www.johnlange.ca</a> &lt;<a href="http://www.johnlange.ca" target="_blank">http://www.johnlange.ca</a>&gt; &lt;<a href="http://www.johnlange.ca" target="_blank">http://www.johnlange.ca</a>&gt;<br>

<div class="im">&gt;<br>
&gt;     --<br>
&gt;     Gilles R. Detillieux              E-mail: &lt;<a href="mailto:grdetil@scrc.umanitoba.ca">grdetil@scrc.umanitoba.ca</a><br>
</div>&gt;     &lt;mailto:<a href="mailto:grdetil@scrc.umanitoba.ca">grdetil@scrc.umanitoba.ca</a>&gt;&gt;<br>
<div class="im">&gt;     Spinal Cord Research Centre       WWW:    <a href="http://www.scrc.umanitoba.ca/" target="_blank">http://www.scrc.umanitoba.ca/</a><br>
&gt;     Dept. Physiology, U. of Manitoba  Winnipeg, MB  R3E 0J9  (Canada)<br>
&gt;     _______________________________________________<br>
&gt;     Roundtable mailing list<br>
</div>&gt;     <a href="mailto:Roundtable@muug.mb.ca">Roundtable@muug.mb.ca</a> &lt;mailto:<a href="mailto:Roundtable@muug.mb.ca">Roundtable@muug.mb.ca</a>&gt;<br>
<div class="im">&gt;     <a href="http://www.muug.mb.ca/mailman/listinfo/roundtable" target="_blank">http://www.muug.mb.ca/mailman/listinfo/roundtable</a><br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; --<br>
</div>&gt; Sean Walberg &lt;<a href="mailto:sean@ertw.com">sean@ertw.com</a> &lt;mailto:<a href="mailto:sean@ertw.com">sean@ertw.com</a>&gt;&gt;    <a href="http://ertw.com/" target="_blank">http://ertw.com/</a><br>
&gt;<br>
&gt;<br>
&gt; ------------------------------------------------------------------------<br>
<div><div></div><div class="h5">&gt;<br>
&gt; _______________________________________________<br>
&gt; Roundtable mailing list<br>
&gt; <a href="mailto:Roundtable@muug.mb.ca">Roundtable@muug.mb.ca</a><br>
&gt; <a href="http://www.muug.mb.ca/mailman/listinfo/roundtable" target="_blank">http://www.muug.mb.ca/mailman/listinfo/roundtable</a><br>
<br>
--<br>
Gilles R. Detillieux              E-mail: &lt;<a href="mailto:grdetil@scrc.umanitoba.ca">grdetil@scrc.umanitoba.ca</a>&gt;<br>
Spinal Cord Research Centre       WWW:    <a href="http://www.scrc.umanitoba.ca/" target="_blank">http://www.scrc.umanitoba.ca/</a><br>
Dept. Physiology, U. of Manitoba  Winnipeg, MB  R3E 0J9  (Canada)<br>
_______________________________________________<br>
Roundtable mailing list<br>
<a href="mailto:Roundtable@muug.mb.ca">Roundtable@muug.mb.ca</a><br>
<a href="http://www.muug.mb.ca/mailman/listinfo/roundtable" target="_blank">http://www.muug.mb.ca/mailman/listinfo/roundtable</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>Sean Walberg &lt;<a href="mailto:sean@ertw.com">sean@ertw.com</a>&gt;    <a href="http://ertw.com/">http://ertw.com/</a><br>