The following script search file(s) for a string (ereg specification) and print context lines (before and after).
Usage:
grep_with_context [ -v before=before_lines ] [ -v after=after_lines ]eregfile... before_lines : number of lines to display after line containing ereg after_lines : number of lines to display after line containing ereg ereg : searched string as a regular expression file.. : input file(s)
tikual,
> BEGIN{c=0} # Implicit
c is a flag saying if we have to print current line
> /search string/{c=11}
When "search string" is found, put in c the number of adjacent lines you want to output: the matching line + the 10 following
> c{print $0;c--}
If c is not evaluated as zero, print the current line and decrement c
Great, good explanation! I never know that 'c' in c{print $0;... is a test on c value. Only know "if ( c > 0 )". Now I know that is a short term. Ygor and you are expert in awk. I don't think that I can reach your ability by reading my learning book.
Your awk script works fine only for the first line matching 'foo'.
Since the counter is never reset, all the remaining of the file is printed starting with the second line matching 'foo'
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.