I recently compared various string replacement methods, including the user program sed -e , perl -lnpe and probably the not-so-well-known MySQL command-line utility, replace . replace , optimized for replacing strings, was almost an order of magnitude faster than sed . The results looked something like this: slower at first:
custom program > sed > LANG=C sed > perl > LANG=C perl > replace
If you need performance, use replace . For it to be available on your system, you will need to install some MySQL distribution.
From replace.c :
Replace lines in a text file
This program replaces lines in files or from stdin to stdout. It takes a list of from-string / to-string strings and replaces each occurrence of a string from a string with the corresponding string. The first match of the found string is consistent. If there is more than one possibility of replacing a string, longer matches are preferable to shorter matches.
...
Programs make a DFA-state-machine of lines, and the speed does not depend on the number of replacement lines (only the number of replacements). The string is supposed to end with \ n or \ 0. There are no restrictions on the number of lines for line lengths.
Read more about sed. You can use multiple kernels with sed by splitting your replacements into #cpus groups and then pass them through sed commands, something like this:
$ sed -e 's/A/B/g; ...' file.txt | \ sed -e 's/B/C/g; ...' | \ sed -e 's/C/D/g; ...' | \ sed -e 's/D/E/g; ...' > out
In addition, if you use sed or perl , and your system has UTF-8 configuration, then this also improves performance to place LANG=C before the commands:
$ LANG=C sed ...
source share