in reply to Help editing a file based off seach criteria

When manipulating text files, one approach would be to use Tie::File. Another approach would be to rewrite the file yourself by using $^I.

Without seeing what code you already have, it's somewhat hard to suggest the best approach. So help us to help you, by providing the code you already have and telling us where you have encountered problems.

  • Comment on Re: Help editing a file based off seach criteria

Replies are listed 'Best First'.
Re^2: Help editing a file based off seach criteria
by perlnewb123 (Sexton) on Jan 12, 2009 at 20:02 UTC
    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?

      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);