tony wrote:
Hello,

Hello,

gnu grep -C allows for output in context to the matched line
is there an updated way to do this in perl using $. == $linenumber +
context?
I looked into the other thread that mentioned it, but none of the
scripts worked.

I tried the -00 switch and regex backreferences with $1, etc but
thought there must be an easier way.
more importantly,
Is there a way to use such a match as the replacement pattern for a
different line?

Ideally the substitution would be like:
$. == $matchedLineNumber +/- $contextNumber and s/$patternInMatched/
$newPattern/ and print $_;

Here's an example:

I want to edit specific lines in a file using an array of the specific
line numbers.
But I don't know how to reference an element of the array when I use
an implicit loop of the lines in the file from the -p or -n switch.

print line numbers and lines of the file, test.txt

$ perl -wnl -e 'print "$. $_";' test.txt
1 animal cat
2 tag this line
3 animal dog
4 tag this line
5 animal cat
6 tag this line
7

I want to substitute a string in the lines with 'tag' with the
replacement string from the previous line something like:

Do you mean something like this:

$ echo "animal cat
tag this line
animal dog
tag this line
animal cat
tag this line
" | perl -pe'$x=s/^(tag ).*/$1$x/s||$_'
animal cat
tag animal cat
animal dog
tag animal dog
animal cat
tag animal cat



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to