in reply to Re: Help editing a file based off seach criteria
in thread Help editing a file based off seach criteria

Here is how I find the string in the file:
#!/usr/bin/perl open DATA, "</home/file.conf"; my @list = grep /\END\b/, <DATA>; chomp @list; print "$_\n" foreach @list;
How do I take the starting position, of the file where END is found (only will occur one time), and move up one position to write to the file?

Replies are listed 'Best First'.
Re^3: Help editing a file based off seach criteria
by Corion (Patriarch) on Jan 12, 2009 at 20:07 UTC

    You could use a loop instead of using grep, and keep a counter to remember the line number, or just remember not only the current line but also the line before it.

    my @list = <DATA>; chomp @list; my $prev_line; for my $line (@list) { # Look at current line # remember this line as "previous" };
      Same thing here, this tells me each time what line the search criteria will be in, even when the file grows, but how do I jump up one line if the pattern is matched?
      #!/usr/bin/perl open DATA, "</home/file.conf"; while (<DATA>) { if(m/\END\b/) { print "$.\n"; } } close(DATA);

        Now, what line number could the line have that is right before the current line (whose number you know)?