MLUG: RE: [MLUG] grepping the 9th line after a matching line
RE: [MLUG] grepping the 9th line after a matching line
Email address obfuscation in effect -- please click here to turn it off.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
nevermind... wouldn't work for multiple matches *duh*

Charlie Huggard | Research Assistant & Systems Administrator | Computational Intelligence Research Lab | University of Missouri - Columbia | EMAIL:PROTECTED | 314/591-0087 | http://cirl.missouri.edu | http://www.missouri.edu



-----Original Message-----
From: EMAIL:PROTECTED on behalf of Huggard, Arthur Charles (UMC-Student)
Sent: Thu 03-May-07 20:04
To: MLUG Members
Subject: RE: [MLUG] grepping the 9th line after a matching line
 
Correct me if I'm being too simplistic about this... but would 

egrep -A9 [pattern] file | tail -n1 

work?

Charlie Huggard | Research Assistant & Systems Administrator | Computational Intelligence Research Lab | University of Missouri - Columbia | EMAIL:PROTECTED | 314/591-0087 | http://cirl.missouri.edu | http://www.missouri.edu



-----Original Message-----
From: EMAIL:PROTECTED on behalf of Mike Miller
Sent: Thu 03-May-07 16:17
To: MLUG membership
Subject: [MLUG] grepping the 9th line after a matching line
 
You guys have been absolutely amazing on that list question about copying 
files.  So now I have another question:

Here's a fun problem -- suppose I want to grab the 9th line following a 
line that matches a pattern.  I think perl may be good for this -- I'm 
pretty sure I can do it as a one-liner.  But there are "-A" and "-B" 
options in the GNU version of egrep that simplify this a little.  The "B" 
and "A" stand for "before" and "after."  So...

egrep -A9 [pattern] file

...grabs the matching lines and 9 subsequent lines for each match.  The 
problem is that I don't want the first 8 lines from each match. 
Fortunately, egrep -A gives me a line between each set of 9 that matches 
the following regexp pattern:  ^--$  That is, it is a single line 
consisting of nothing but two hyphens.  So this gives me the 9th line 
following each match but with that darned pair of hyphens still there...

egrep -A9 [pattern] file | egrep -B1 '^--$'

...so I grep out the extra hyphen lines:

egrep -A9 [pattern] file | egrep -B1 '^--$' | egrep -v '^--$'

That's fairly straightforward but there is one more annoying problem: 
The last line is not followed by the ^--$ regular expression, so it is 
lost.  Well, then I can do this but this is getting rather long:

( egrep -A9 [pattern] file | egrep -B1 '^--$' | egrep -v '^--$' ; egrep -A9 [pattern] file | tail -1 )

Long, but it works except under conditions where the sets of 9 lines 
overlap!  Then it totally screws up.  Luckily, that condition does not 
arise in my application, so I'm fine with this.

Question:  Is there a better way?

Mike

_______________________________________________
members mailing list
EMAIL:PROTECTED
http://mlug.missouri.edu/mailman/listinfo/members


<<winmail.dat>>

_______________________________________________
members mailing list
EMAIL:PROTECTED
http://mlug.missouri.edu/mailman/listinfo/members