GaijinPunch has asked for the wisdom of the Perl Monks concerning the following question:
Basically what I want to do is go line by line through my file, if I hit a specific string, take the next few lines, looking for another string. If I find the 2nd string, I'll need to check the next 10 lines or so for another string, then go BACK 1 line ahead of where I matched my first string, and start the whole process again. I've gotten up to matching the first two strings, but my SEEK always goes back to the first of the file.
It looks to me that $. doesn't give the real line number, but just counts how many lines Perl has run through. Anyways, any help is appreciated.
Since I can't print out $position{$.+1} I can't tell if it's seeking properly - ever. But, only the the last seek at the bottom of the file is giving me an 'unitialized value'.#!/usr/bin/perl # # use strict; use warnings; my $file = "file.log"; open ( IN, $file ) || die "Can't open $file $!"; my %position; while ( <IN> ) { chomp; $position{$.+1} = tell(IN); #I don't quite grasp this part yet # $. holds line number if ( /first_string/ ) { my $count = 0; #bad way of only finding 2nd string once print "$. $_\n"; seek IN, $position{$.+1}, 0; while ( <IN> ) { chomp; if ( ( $_ =~ /2nd_string/ ) && ( $count < 1 ) ) { print "found 2nd string\n"; $count += 1; } } } seek IN, $position{$.+1}, 0; } close FILE;
Edited by castaway - changed literal html link text to an id:// link
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problem with SEEK to go back/forward a few lines.
by pg (Canon) on Oct 16, 2003 at 02:40 UTC | |
by GaijinPunch (Pilgrim) on Oct 16, 2003 at 02:49 UTC | |
|
Re: Problem with SEEK to go back/forward a few lines.
by Fletch (Bishop) on Oct 16, 2003 at 02:17 UTC | |
|
Re: Problem with SEEK to go back/forward a few lines.
by Roger (Parson) on Oct 16, 2003 at 02:37 UTC | |
|
Re: Problem with SEEK to go back/forward a few lines.
by sgifford (Prior) on Oct 16, 2003 at 06:36 UTC | |
by parv (Parson) on Oct 20, 2003 at 04:16 UTC |