#!/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 () { $match = 1 if $_ =~ /baz/; if ($match && ! $replaced) { print "Replacement Line\n$_"; ($match, $replaced) = (0, 1); } else { print; } }