Replacing text in place with GNU sed

sed is a stream editor, which means that it accepts a stream of text, processes it, and spits out another stream of text.  sed can process files that are too large to load into memory, and it is a completely command-line tool that can easily be integrated into shell scripts.  This excellent sed tutorial was written for an old version of sed provided by Sun Microsystems, and it doesn’t cover one of the most useful features of GNU sed.  GNU sed accepts a -i command line argument that tells sed to replace the text file in place, rather than writing the output stream to another file.  Another nice feature is that sed -i will create a backup file before processing if you provide a backup suffix:

sed -i.bak 's/CB_0/CAB_max/g'

Briefly, the sed command in this example works like this: s tells sed to substitute CB_0 with CAB_max, and the g (which stands for global) tells sed to replace every occurrence in the file (you can also process multiply files with wildcards like *.py).  I used this example to change a variable name in a bunch of similar Python scripts.
I later learned that you can do the same thing with perl, using almost the same syntax:

perl -i.bak -pe 's/CB_0/CAB_max/g' *.py

The -e option tells perl to execute the one-line script that is provided, and the -p option tells perl to iterate over the file name arguments.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.