Printing 10 lines above and below the search string: help needed

Hi,

The below code will search a particular string(say false in this case) and return me 10 lines above and below the search string in a file.

"

awk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print("***********************************");print;c=a;}b{r[NR%b]=$
0}' b=10 a=10 s="false" 

"

Further to enhance I want to ignore if the same search string appears in those 10 lines. (Currently this is not happening. For Ex: If the search string exists in 5th line it again starts printing 10lines above and below causing some mess.)

It would be really great if someone could help me on this.

P.S: Being my first post apologies for any mistakes in my post and suggestions welcome.

Thanks,
Vimal

Does your version of grep support

grep -A 10 -B 10 'mypattern'   filename

That does exaclty what you want.

Hi,

Thanks a lot but I m afraid that when i execute the above command it says "-A" not a valid option.

Please suggest me a different option to try with.

in solaris use below

/usr/sfw/bin/ggrep -A 10 -B 10 'mypattern'   filename

Try this one:

awk '
/false/ {
  for(i=0;i<10;i++) {
    print a[(NR+i)%10]
  }
  exit
}
{a[NR%10]=$0}
' file

Dear Friend,

You can use the following code also

 
  egrep -A 10 -B 10 "pattern" file 
 

Here

-A is used to print the 10 lines after the matched patten
-B is used to print the 10 lines before the matched pattern