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]
On Thu, 3 May 2007, ryan woodsmall wrote:

Hmm:

 ( egrep -A9 [pattern] file ; echo "--" ) | egrep -B1 ^--$ | egrep -v ^--$

Works? The echo command throws a trailing "--" at the end of the first egrep output, so your input is all the same...

Yes! That's a good way to do it. I was actually thinking to myself "how can I add one at the end?" but blocked on the subshell idea (use of parentheses), even though I came up with a (much lamer) subshell-based idea later. So that solves one problem. I will use your idea.


But suppose you want to grab the third line after every line with an 'X' in it. Then we'd want to grab lines with 'match' here because they are three after an X:

X
X
skip
match
match
skip

But look what happens:

# cat > foo
X
X
skip
match
match
skip
^D

# egrep -A3 X foo
X
X
skip
match
match


See? When the second X falls within the three lines following the first X, it just extends the selected region to a fourth line.


This isn't a problem I have to solve because my regions are non-overlapping, but this is potentially a problem for things someone might do some day! ;-)

Mike

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