if ($remove eq ~ m/^.*\b($remove)\b.*$) { # do stuff here } #### sub clean_the_file { my $sourcefile = "/u/ccsys/CC_print.printers"; # grab the sourcefile and the string to remove as the first and second arguments my ( $sourcefile, $remove ) = @_; # create a temporary file my $tempfile = # Some random file creation logic; # extract lines from $sourcefile which do NOT have the string to remove # note that the \' inserts ticks so that strings with spaces can work, too `grep -v \'$remove\' $sourcefile\n > $tempfile`; # delete source file, then rename working file unlink($sourcefile); rename( $tempfile, $sourcefile ); }