Email address obfuscation in effect -- please
click here to turn it off.
[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
Not a one-liner, but how about this perlishness:
#!/usr/bin/perl
$pos = $ARGV[0];
$pat = $ARGV[1];
push(@lines, $line) while($line = <STDIN>);
for($i = 0; $i < $#lines; $i++){
print("$lines[$i + $pos]") if($lines[$i] =~ m/$pat/);
}
Call it with an "after" position and a pattern:
perl whateveryounameit.pl 9 "[regex]" < inputfile
Should get you what you want. You could hardcode the position/
pattern in for a one-liner. This will eat a *ton* of memory if you
have a big input file, as it reads everything into an array. So be
careful, no warranty, yada yada yada.
You could do it with regular fread()/sysread()/etc., and seek() ahead
then rewind back, but that's a bit trickier.
I'm in the process of (slowly) learning Python - anyone have an
elegant pythonic solution to this problem?
ryan woodsmall
EMAIL:PROTECTED
"Be well, do good work, and keep in touch." - Garrison Keillor
On May 3, 2007, at 4:52 PM, Mike Miller wrote:
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
_______________________________________________
members mailing list
EMAIL:PROTECTED
http://mlug.missouri.edu/mailman/listinfo/members