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]
ryan woodsmall wrote:
and perl, but i'm sure someone else could do better:

perl -e 'while($l=<STDIN>){if($l=~m/[PATTERN]/){for($i=0;$i<9;$i++){$l=<STDIN>;}print("$l");}}'


perl -lne '$f=-10 if /[PATTERN]/;$f++;print if $f==0' file





yup.

  ryan woodsmall
    EMAIL:PROTECTED


"Be well, do good work, and keep in touch." - Garrison Keillor




On May 3, 2007, at 4:17 PM, Mike Miller wrote:

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


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




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