in reply to insert line of text above a line
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.#!/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; } }
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 |