tony wrote:
> 
> 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:
> 
> long way
> 
> # get the line numbers of the lines you want the substitution to occur
> $ perl -wnl -e '/tag/ and print "$. $_";' test.txt
> 2 tag this line
> 4 tag this line
> 6 tag this line
> 
> #then code a substitution line for each line; manually looking at
> previous line
> 
> #!/usr/bin/env perl -i.old -wpl
> $. == 2 and s/this line/cat animal/;
> $. == 4 and s/this line/dog animal/;
> $. == 6 and s/this line/cat animal/;
> 
> instead I want to use an array and loop over the lines in the file
> rather than line by line, but
> if <STDIN> is in the implicit array from the -p switch, how do I
> reference it?
> 
> 
> #!/usr/bin/env perl -i.old -wpl
> @array = (2,4,6)
> $. = $array[what goes here?] and s/this line/cat animal/
> 
> I also tried
> 
> #!/usr/bin/env perl -i.old -wpl
> @array = (2,4,6)
> while (<>){
>       $. = $array[what goes here?] and s/this line/cat animal/
> }
> 
> it will work if I specify one line at a time as an argument and I know
> beforehand that the line in context is +1 from the matched line:
> 
> #!/usr/bin/env perl -s -i.old -wpl
> # beforehand find all line numbers that have the pattern
> BEGIN {
>       $ln or warn "Enter line: $0 -ln\n" and exit 255
> }
>       # sample nearby -1 lines that need substitution @lines =
> (7303,7309,7315,7321,7327,7333,7339,7345,7351,7357,7363,7369,7375,7381,7387,7393,7399,7405,7411,7417,7423,7429,7435,7441,7447,7453,7459,7465,7471,7477,7483,7489,7495,7501,7507,7513,7519,7525,7531);
>       $. == $ln + 1 and s/this line/$pattern/g;
> 
> Any thoughts or suggestions appreciated!

Your description of what you want to do is very unclear. First you say you want 
to

- You can't assign to $. It's a read-only variable that holds the number of the
lines read from the file handle you most recently accessed.

- <STDIN> isn't an implicit array. Where did you read that? It's a read
operattion on the STDIN file handle.

The part I do understand is

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

Does this program help?

Rob



use strict;
use warnings;

my $prev = '';
while (<DATA>) {
  chomp;
  if (/tag/) {
    s/this line/$prev/;
  }
  else {
    $prev= $_;
  }
  print "$_\n";
}

__END__
animal cat
tag this line
animal dog
tag this line
animal cat
tag this line

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


Reply via email to