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

May 28, 2007 at 5:59 pm

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’ *

Category: Systems 101

2 Comments
Gravatar

Comment by Stephan Beal

Made Tuesday, 2 of October , 2007 at 12:32 am

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

Gravatar

Comment by Phil

Made Tuesday, 2 of October , 2007 at 12:48 am

Thanks for the -i sed tidbit..

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>