String Substitution in PERL vs SED Removing Disk I/O Factor

May 28, 2007

A lot of systems administrators will use SED for string substitution, when they should try using PERL. When doing string substitutions with SED the string substitutions write to a seperate file, then the file is copied over the target file. This second file that is created utilizes Disk I/O, another issue you can run into are file permissions if your umask doesn’t match the permissions of the target file. With PERL you do not have any of these issues since its effects are directly onto the target file with no usage of secondary files therefore not Disk I/O.

To run string substitution globally to the target file you can do the following:

perl -pi -e 's{original string}{replacement string}g' targetfile

To run string substitution globally to the multiple files in the directory you can do the following:

perl -pi -e 's{original string}{replacement string}g' *

Comments for “String Substitution in PERL vs SED Removing Disk I/O Factor”

  1. FYI: recent versions of GNU sed also have an -i option to edit a file in-place.

  2. Phil commented on October 2, 2007

    Thanks for the -i sed tidbit..