Advanced grep unix Advanced grep unix unix unix

Advanced grep unix


Yes, use

grep -B num1 -A num2 

to include num1 lines of context before the match, and num2 lines of context after the match.

EDIT:

Seems the OP is using AIX. This has a different set of options which doesn't include -B and -A

this link describes grep on AIX 4.3 (it doesn't look promising)

Matt's perl script might be a better solution.


Here is what I usually do on AIX:

before=2  << The number of lines to be shown Before >>after=2   << The number of lines to be shown After >>grep -n <pattern> <filename> | cut -d':' -f1 |  xargs  -n1 -I % awk "NR<=%+$after && NR>=%-$before" <filename>

If you do not want the extra 2 varialbles you can always use it an a one line:

grep -n <pattern> <filename> | cut -d':' -f1 |  xargs  -n1 -I % awk 'NR<=%+<<after>> && NR>=%-<<before>>' <filename>

Suppose I have a pattern 'stack' and the filename is flow.txtI want 2 lines before and 3 lines after. The the command will be like:

grep -n 'stack' flow.txt | cut -d':' -f1 |  xargs  -n1 -I % awk 'NR<=%+3 && NR>=%-2' flow.txt

I want 2 lines before and only - the the command will be like:

grep -n 'stack' flow.txt | cut -d':' -f1 |  xargs  -n1 -I % awk 'NR<=% && NR>=%-2' flow.txt

I want 3 lines after and only - the the command will be like:

grep -n 'stack' flow.txt | cut -d':' -f1 |  xargs  -n1 -I % awk 'NR<=%+3 && NR>=%' flow.txt

Multiple Files - change it for Awk & grep. From above for the pattern 'stack' with the filename is flow.* - 2 lines before and 3 lines after. The the command will be like:

    awk 'BEGIN {     before=1; after=3; pattern="stack";    i=0; hold[before]=""; afterprints=0}    {     #Print the lines from the previous Match    if (afterprints > 0)      {      print FILENAME ":" FNR ":" $0      afterprints-- #keep a track of the lines to print after - this can be reset if a match is found      if (afterprints == 0) print "---"      }    #Look for the pattern in current line    if ( match($0, pattern) > 0 )      {      # print the lines in the hold round robin buffer from the current line to line-1      #  if (before >0)  => user wants lines before avoid divide by 0 in %      #  and afterprints => 0 - we have not printed the line already      for(j=i; j < i+before && before > 0 && afterprints == 0 ; j++)        print hold[j%before]      if (afterprints == 0) #  print the line if we have not printed the line already        print FILENAME ":" FNR ":" $0      afterprints=after      }    if (before > 0) # Store the lines in the round robin hold buffer      { hold[i]=FILENAME ":" FNR ":" $0        i=(i+1)%before }  }' flow.*


From the tags, it's likely that the system has a grep that may not support providing context (Solaris is one system that doesn't and I can't remember about AIX). If that is the case, there's a perl script that may help at http://www.sun.com/bigadmin/jsp/descFile.jsp?url=descAll/cgrep__context_grep.