| Brad Baxter 2006-01-10, 4:02 am |
| czimmer@wczimmerman.dyndns.org wrote:
> I need to search for a string in a large text file and print from the
> matching line + the 50 lines afterwards. I'm getting an "Use of
> uninitialized value at line 9" but I cannot figure out why. I know
> that my $begin and $end variables are working as I ran the script with
> print statements to verify them. What am I missing?
>
> #!/usr/bin/perl -w
> use strict;
> open(OUTPUT,"output") || die "Cannot open! \n";
> while (my$line = <OUTPUT> )
> {
> if ($line =~ /String to search for/) {
> my$begin = $.;
> my$end=($begin + 50);
> print if $begin .. $end;
> }
> }
> close (OUTPUT);
You are printing $_ (which is uninitialized) instead of $line.
Your logic is flawed, too, I'm afraid; it won't do what you want,
because you only print when $line matches.
perl -ne'($l=$.+50,$f++)if/String to search for/;$f..$.<=$l&&print'
output
But that might give you unexpected results if the string to search
for appears on one of the 50 lines.
--
Brad
|