#!/usr/bin/perl -i.bak -w @ARGV = ('c:/test.txt'); # set our file to process while (<>) { print "newline\n" if m/somecond/; # add a new line is line matches somecond s/this/that/; # substitute this for that on every line print; # write the modified (or not) lines }