0

I want to see 5 lines befor and after the 'Keyword',but i am getting this error. I am working on HP-UNIX. Can any one help me with this one? Thanks

Command:grep -C 5 Keyword file_name

Error:

grep: illegal option -- C
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] -e pattern_list...
        [-f pattern_file...] [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] [-e pattern_list...]
        -f pattern_file... [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] pattern [file...]

1 Answers1

0

Here is an awk that emulate grep -C 5

awk '{a[NR]=$0} $0~s {f=NR} END {for (i=f-C;i<=f+C;i++) print a[i]}' C=5 s="Keyword" file_name

PS! does only work with one hit.


This should emulate grep -C 5 for more hits. (some complicate due to array sort missing)

awk '{a[NR]=$0} $0~s {f[NR]++} END {for (j=1;j<=NR;j++) if (f[j]) for (i=j-C;i<=j+C;i++) print a[i]}' C=5 s="Keyword" file_name
Jotne
  • 40,548
  • 12
  • 51
  • 55
  • You are welcome. If you like the answer, you can accept it by click on the check mark to the left of the post :) – Jotne Jan 07 '14 at 14:10