Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find string & grab next 10 lines... 8

Status
Not open for further replies.

isileth

Technical User
Jul 18, 2001
9
US

OK, am searching a file for a certain text string - when I find it, I need to output the line it's in, and the following 10 lines?

Any help much appreciated.
 
Scruffy way :
awk '/your String/ {print; getline; print; getline; print; print; getline;print; getline;print; getline; print; getline; print; print; getline;print; getline;print }' filename > newfile


Dickie Bird (:)-)))
 
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 ]ereg file...
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)

Example:
grep_with_context -v after=10 'ERROR' input_file

[tt]
------ grep_with_context ------
#!/usr/bin/awk -f

function PrintBeforeContext ( lindex, lfrom, lto) {
lfrom = before_index - before;
if (lfrom < 0) lfrom = 0 ;
lto = before_index - 1 ;
for ( lindex = lfrom ; lindex <= lto ; lindex++ ) {
print before_context[lindex % before] ;
}
before_index = 0 ;
}

BEGIN {
before = before + 0 ;
after = after + 0 ;
before_index = 0 ;
before_context[0] = &quot;&quot;;
after_index = 0 ;
pattern = ARGV[1];
ARGV[1] = &quot;&quot;;
if (ARGC <= 2) ARGV[ARGC++] = &quot;-&quot;;
}

$0 ~ pattern { PrintBeforeContext() ;
print $0 ;
after_index = after ;
next ;
}

after_index > 0 {
print $0 ;
after_index-- ;
next ;
}

before > 0 {
before_context[before_index % before] = $0 ;
before_index++ ;
}
------ grep_with_context ------
[/tt]


Jean Pierre.
 
Try...

awk '/search string/{c=11}c{print $0,c--}' file1
 
Sorry, the comma should be a semi-colon...

awk '/search string/{c=11}c{print $0;c--}' file1
 
If you have GNU-grep, you can use the -A 10 option

Hope This Help
PH.
 
awk '/search string/{c=11}c{print $0;c--}' file1

How it work? Just like print previous line example in before. I never seen this structure from learning book. Do anyone explain it to me? Thanks!!

tikual
 
tikual,
> BEGIN{c=0} # Implicit
c is a flag saying if we have to print current line
> /search string/{c=11}
When &quot;search string&quot; 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

Hope This Help
PH.
 
Great, good explanation! I never know that 'c' in c{print $0;... is a test on c value. Only know &quot;if ( c > 0 )&quot;. 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.

Really appreciate for your help. A star for you!

tikual
 

Many thanks for the help - worked perfectly.
 
shorter ...

awk '/search string/{c=11} c--' file1

Jean Pierre.
 
Please, ignore my preceding post. That does'nt work.

Jean Pierre.
 
the right command

awk '/search string/{c=11} c-->0' file1

Jean Pierre.
 
Hi Aigles,

You are also attentive to newbie of awk just like me. A star for you too!

Cheers

tikual
 
Please excuse my ignorance, new to awk. Is there a way to modify this to only print from a line after the search string has been found

I have a file like this
##$D= (0-31)
0 2 3 4 5

And I would like to extract the value 2.

doing awk '/\$D=/ {c=2}c-->0 {print $2}' <file>

gives me:
(0-31)
2

thank

Matt
 
Untested, but this might do it

awk '/\$D=/ {c=1;next}c-->0 {print $2}' <file>

or

awk '/\$D=/ {c=1;next} c{print $2;c=0}' <file>



CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
To print matching line and next 10 lines:
Code:
/foo/,c==10{print;c++}
 
Hi fururelet,

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'

Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top