sub clean_the_file { my $sourcefile = "/u/ccsys/CC_print.printers"; # Keep a dedicated backup of CC_print.printers if ( !-t "/root/CC_print.printers.bkp" ) { system("touch /root/CC_print.printers.bkp"); system("cp -R $sourcefile /root/CC_print.printer.bkp"); } if ( -t "/root/CC_print.printers.bkp" ) { system("cp -R $sourcefile /root/CC_print.printer.bkp"); # grab the sourcefile and the string to remove as the first and second arguments my ( $sourcefile, $removeq ) = @_; # create a temporary file system("touch /u/ccsys/CC_print.printers.bkp_mkpq"); my $tempfile = "/u/ccsys/CC_print.printers.bkp_mkpq"; # 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 $removeq $sourcefile > $tempfile`; sleep 1; print "Waiting to unlink..\n"; # delete source file, then rename working file unlink($sourcefile); rename( $tempfile, $sourcefile ); system("rm -rf $tempfile"); } }