<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: grep for string but include lines above and below in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/grep-for-string-but-include-lines-above-and-below/m-p/3703435#M496841</link>
    <description>To John Korterman&lt;BR /&gt;&lt;BR /&gt;John .... did you omit to attach the sample script find_bef_after.sh  ?  &lt;BR /&gt;&lt;BR /&gt;Thanks</description>
    <pubDate>Thu, 05 Jan 2006 09:47:30 GMT</pubDate>
    <dc:creator>Derek Brown</dc:creator>
    <dc:date>2006-01-05T09:47:30Z</dc:date>
    <item>
      <title>grep for string but include lines above and below</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/grep-for-string-but-include-lines-above-and-below/m-p/3703431#M496837</link>
      <description>&lt;P&gt;Hi, This forum has always been great for me and I wonder if anyone can solve this one for me please ?&lt;BR /&gt;&lt;BR /&gt;I need to search a file for a specific string. Easy enough with grep e.g. &lt;BR /&gt;&lt;BR /&gt;# grep root /etc/passwd&lt;BR /&gt;&lt;BR /&gt;BUT .... I need the answer to provide the 3 lines above the string and also the 2 lines below the string. &lt;BR /&gt;&lt;BR /&gt;Linux has this built in as a feature but is the same thing possible in standard HPUX ? &lt;BR /&gt;&lt;BR /&gt;Many thanks.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;-------------------------------&lt;BR /&gt;&lt;BR /&gt;P.S. This Thread has been moved from HP-UX --&amp;gt; System Administration to HP-UX --&amp;gt; Languages &amp;amp; Scripting - Forum Moderator&lt;/P&gt;</description>
      <pubDate>Wed, 01 Feb 2012 12:16:58 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/grep-for-string-but-include-lines-above-and-below/m-p/3703431#M496837</guid>
      <dc:creator>Derek Brown</dc:creator>
      <dc:date>2012-02-01T12:16:58Z</dc:date>
    </item>
    <item>
      <title>Re: grep for string but include lines above and below</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/grep-for-string-but-include-lines-above-and-below/m-p/3703432#M496838</link>
      <description>Derek,&lt;BR /&gt;&lt;BR /&gt;From "Handy One-Liners for Sed" (attached):&lt;BR /&gt;&lt;BR /&gt;# print 1 line of context before and after regexp, with line number&lt;BR /&gt; # indicating where the regexp occurred (similar to "grep -A1 -B1")&lt;BR /&gt; sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Pete</description>
      <pubDate>Thu, 05 Jan 2006 08:10:52 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/grep-for-string-but-include-lines-above-and-below/m-p/3703432#M496838</guid>
      <dc:creator>Pete Randall</dc:creator>
      <dc:date>2006-01-05T08:10:52Z</dc:date>
    </item>
    <item>
      <title>Re: grep for string but include lines above and below</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/grep-for-string-but-include-lines-above-and-below/m-p/3703433#M496839</link>
      <description>Derek,&lt;BR /&gt;please have a look at:&lt;BR /&gt;&lt;A href="http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=861863" target="_blank"&gt;http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=861863&lt;/A&gt;&lt;BR /&gt;Seems for multiple lines you require gnu grep.&lt;BR /&gt;</description>
      <pubDate>Thu, 05 Jan 2006 08:24:25 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/grep-for-string-but-include-lines-above-and-below/m-p/3703433#M496839</guid>
      <dc:creator>Peter Godron</dc:creator>
      <dc:date>2006-01-05T08:24:25Z</dc:date>
    </item>
    <item>
      <title>Re: grep for string but include lines above and below</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/grep-for-string-but-include-lines-above-and-below/m-p/3703434#M496840</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;try the attached script, find_bef_after.sh&lt;BR /&gt;using $1=string to grep for&lt;BR /&gt;$2=number of lines before and after string&lt;BR /&gt;$3=path to file&lt;BR /&gt;&lt;BR /&gt;slow, but worked the last time I tried..&lt;BR /&gt;&lt;BR /&gt;regards,&lt;BR /&gt;John K&lt;BR /&gt;</description>
      <pubDate>Thu, 05 Jan 2006 08:49:27 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/grep-for-string-but-include-lines-above-and-below/m-p/3703434#M496840</guid>
      <dc:creator>john korterman</dc:creator>
      <dc:date>2006-01-05T08:49:27Z</dc:date>
    </item>
    <item>
      <title>Re: grep for string but include lines above and below</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/grep-for-string-but-include-lines-above-and-below/m-p/3703435#M496841</link>
      <description>To John Korterman&lt;BR /&gt;&lt;BR /&gt;John .... did you omit to attach the sample script find_bef_after.sh  ?  &lt;BR /&gt;&lt;BR /&gt;Thanks</description>
      <pubDate>Thu, 05 Jan 2006 09:47:30 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/grep-for-string-but-include-lines-above-and-below/m-p/3703435#M496841</guid>
      <dc:creator>Derek Brown</dc:creator>
      <dc:date>2006-01-05T09:47:30Z</dc:date>
    </item>
    <item>
      <title>Re: grep for string but include lines above and below</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/grep-for-string-but-include-lines-above-and-below/m-p/3703436#M496842</link>
      <description>sorry, hope it appears now..</description>
      <pubDate>Thu, 05 Jan 2006 12:23:15 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/grep-for-string-but-include-lines-above-and-below/m-p/3703436#M496842</guid>
      <dc:creator>john korterman</dc:creator>
      <dc:date>2006-01-05T12:23:15Z</dc:date>
    </item>
    <item>
      <title>Re: grep for string but include lines above and below</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/grep-for-string-but-include-lines-above-and-below/m-p/3703437#M496843</link>
      <description>Hi Derek:&lt;BR /&gt;&lt;BR /&gt;How about a perl script like:&lt;BR /&gt;&lt;BR /&gt;# cat ngrep&lt;BR /&gt;#!/usr/bin/perl&lt;BR /&gt;#@(#)ngrep $ Show +-3 lines matching target January 2006 JR_Ferguson $&lt;BR /&gt;&lt;BR /&gt;use strict;&lt;BR /&gt;die "Usage: $0 pattern file\n" unless $#ARGV == 1;&lt;BR /&gt;&lt;BR /&gt;my  $pat  = shift;&lt;BR /&gt;my  $file = shift;&lt;BR /&gt;my  @lines;         &lt;BR /&gt;my  $limit;&lt;BR /&gt;my ($i, $m, $n);&lt;BR /&gt;&lt;BR /&gt;open( FH, "&amp;lt;", $file ) or die "Can't open $file : $!\n";&lt;BR /&gt;@lines = &lt;FH&gt;;&lt;BR /&gt;$limit = $#lines;&lt;BR /&gt;&lt;BR /&gt;foreach (@lines) {&lt;BR /&gt;    if ( m/$pat/ ) {&lt;BR /&gt;        $m = $i-3 &amp;lt; 0      ? 0      : $i-3;&lt;BR /&gt;        $n = $i+3 &amp;gt; $limit ? $limit : $i+3;&lt;BR /&gt;        for ( $m; $m &amp;lt;= $n; $m++ ) {&lt;BR /&gt;            print $lines [$m];&lt;BR /&gt;        }&lt;BR /&gt;        print "\n";&lt;BR /&gt;    }&lt;BR /&gt;    $i++;&lt;BR /&gt;}&lt;BR /&gt;1;&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;/FH&gt;</description>
      <pubDate>Thu, 05 Jan 2006 12:47:27 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/grep-for-string-but-include-lines-above-and-below/m-p/3703437#M496843</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-01-05T12:47:27Z</dc:date>
    </item>
    <item>
      <title>Re: grep for string but include lines above and below</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/grep-for-string-but-include-lines-above-and-below/m-p/3703438#M496844</link>
      <description>Hi again,&lt;BR /&gt;&lt;BR /&gt;a variation taking four parameters - if it ever gets there:&lt;BR /&gt;$1 the string to match&lt;BR /&gt;$2 the number of lines to print before the matched string&lt;BR /&gt;$3 the number of lines to print after the matched string&lt;BR /&gt;$4 the file itself&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;regards,&lt;BR /&gt;John K.</description>
      <pubDate>Thu, 05 Jan 2006 14:36:29 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/grep-for-string-but-include-lines-above-and-below/m-p/3703438#M496844</guid>
      <dc:creator>john korterman</dc:creator>
      <dc:date>2006-01-05T14:36:29Z</dc:date>
    </item>
    <item>
      <title>Re: grep for string but include lines above and below</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/grep-for-string-but-include-lines-above-and-below/m-p/3703439#M496845</link>
      <description>JRF's solution will work fine a a file like /etc/passwd, but as it sucks in the whole file it will become problematic for very large (100+MB?) files.&lt;BR /&gt;&lt;BR /&gt;I prefer a small circular buffer to just retain the last few lines.&lt;BR /&gt;&lt;BR /&gt;For example:&lt;BR /&gt;&lt;BR /&gt;awk 'BEGIN {W=4} {line[i++%W]=$0} /targetstring/{for (j=i-W;j&lt;I&gt;&lt;BR /&gt;Or with a before-and-after:&lt;BR /&gt;awk 'BEGIN {W=5} {line[i++%W]=$0; if (1==x--){for (j=i-W;j&lt;I&gt;&lt;BR /&gt;de-composed:&lt;BR /&gt;BEGIN set window size&lt;BR /&gt;Main loop&lt;BR /&gt;1) move current record into circular buffer&lt;BR /&gt;2) if counter x equals 1, then target was seen 1/2 a window lines ago, start print loop of memory array.&lt;BR /&gt;3) /target/... if target is seen, then set counter to 1/2 the window size.&lt;BR /&gt;&lt;BR /&gt;( This code does NOT handle mutlipel adjacent matches )&lt;BR /&gt; &lt;BR /&gt;I also have posted a perl script in:&lt;BR /&gt;&lt;A href="http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=630972" target="_blank"&gt;http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=630972&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Other prior discussions in:&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=524659" target="_blank"&gt;http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=524659&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Or a little further back in time:&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=224110" target="_blank"&gt;http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=224110&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;btw... &lt;BR /&gt;Under OpenVMS it would be:&lt;BR /&gt; $SEARCH/WINDO=(before,after) file text&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Cheers,&lt;BR /&gt;Hein.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/I&gt;&lt;/I&gt;</description>
      <pubDate>Thu, 05 Jan 2006 17:12:58 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/grep-for-string-but-include-lines-above-and-below/m-p/3703439#M496845</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2006-01-05T17:12:58Z</dc:date>
    </item>
    <item>
      <title>Re: grep for string but include lines above and below</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/grep-for-string-but-include-lines-above-and-below/m-p/3703440#M496846</link>
      <description>Derek,&lt;BR /&gt;&lt;BR /&gt;It can be handled easily with ex as:&lt;BR /&gt;&lt;BR /&gt;# ex -s input_file &amp;lt;&lt;EOF&gt;&lt;/EOF&gt;&amp;gt; /&lt;SEARCH string=""&gt;/-3,/&lt;SEARCH string=""&gt;/+2 p&lt;BR /&gt;&amp;gt; EOF&lt;BR /&gt;&lt;BR /&gt;cheers!&lt;/SEARCH&gt;&lt;/SEARCH&gt;</description>
      <pubDate>Thu, 05 Jan 2006 18:25:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/grep-for-string-but-include-lines-above-and-below/m-p/3703440#M496846</guid>
      <dc:creator>Sandman!</dc:creator>
      <dc:date>2006-01-05T18:25:54Z</dc:date>
    </item>
    <item>
      <title>Re: grep for string but include lines above and below</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/grep-for-string-but-include-lines-above-and-below/m-p/6630404#M496847</link>
      <description>Where is the attached script?</description>
      <pubDate>Thu, 25 Sep 2014 17:53:20 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/grep-for-string-but-include-lines-above-and-below/m-p/6630404#M496847</guid>
      <dc:creator>Sam_kao</dc:creator>
      <dc:date>2014-09-25T17:53:20Z</dc:date>
    </item>
    <item>
      <title>Re: grep for string but include lines above and below</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/grep-for-string-but-include-lines-above-and-below/m-p/6631016#M496848</link>
      <description>&lt;P&gt;&amp;gt;Where is the attached script?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Did you read every reply?&lt;/P&gt;&lt;P&gt;&lt;A href="http://h30499.www3.hp.com/t5/Languages-and-Scripting/grep-for-string-but-include-lines-above-and-below/m-p/3703436#M43222" target="_blank"&gt;http://h30499.www3.hp.com/t5/Languages-and-Scripting/grep-for-string-but-include-lines-above-and-below/m-p/3703436#M43222&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://h30499.www3.hp.com/hpeb/attachments/hpeb/itrc-150/43222/1/270064.sh" target="_blank"&gt;http://h30499.www3.hp.com/hpeb/attachments/hpeb/itrc-150/43222/1/270064.sh&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Sep 2014 09:43:11 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/grep-for-string-but-include-lines-above-and-below/m-p/6631016#M496848</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2014-09-26T09:43:11Z</dc:date>
    </item>
  </channel>
</rss>

