in reply to insert line of text above a line

Glenn,
Assuming you only want to do this on the first match:
#!/usr/bin/perl -w use strict; open (INPUT, "original") or die "Unable to open input file : $!"; open (OUTPUT, ">newfile") or die "Unable to open output file : $!"; select OUTPUT; my ($match, $replaced); while (<INPUT>) { $match = 1 if $_ =~ /baz/; if ($match && ! $replaced) { print "Replacement Line\n$_"; ($match, $replaced) = (0, 1); } else { print; } }
It is then a simple matter of using rename or File::Copy to move the new file in place. You may also want to take a look at Tie::File as it can treat a file as an array.

Cheers - L~R

Replies are listed 'Best First'.
Re: Re: insert line of text above a line
by mabman (Novice) on Sep 25, 2003 at 23:04 UTC
    Excellent! That did it.

    Glenn