#/usr/bin/perl -pi.bak $count++ if s/^go/\//; END { print STDERR "There were $count lines starting with 'go' in the file\n"; } #### open FILE, "< $file" or die "Can't open input file: $!\n"; open OUT, "> '$file.new'" or die "Can't open output file: $!\n"; while () { $count++ if s/^go/\//; print OUT; } close OUT; close FILE; print STDERR "There were $count lines starting with 'go' in the file\n"; rename $file, "$file.bak"; # make backup copy rename "$file.new", $file; # overwrite original file