# open the input file open(IN,"file") or die; # # open a temporary output file ( you could also use the # -i "inplace edit feature", see perldoc perlrun ) # open(OUT,">file.$$") or die; # # print the contents of IN to OUT, # adding your additional line if we # see $dom as the first chars of the line # \Q and \E to quotemeta the domain since # it probably contains metachars like "." # while () { print OUT; if (/^\Q$dom\E\s/) { print OUT "$user\@$dom\t$user\n"; } } # close both files and overwrite # IN with OUT via rename(). close IN; close OUT; rename("file.$$","file") or die;