Logo Questions Linux Laravel Mysql Ubuntu Git Menu

how to speedup following sed command





I have nearly 6000 files to edit using following sed command. its taking longer time.

does any one have an idea how to make following commands faster.

find ./ -type f -exec sed -i -e 's/old word/new word/g' {} \;
like image 686
user3817989 Avatar asked Mar 19 '23 22:03


2 Answers

Similar to @JonathanLeffler's strategy of running one invocation of sed for multiple files, but you can also use xargs to run multiple threads in parallel, taking advantage of multiple processors.

find ./ -type f -print0 | xargs -0 -n 100 -P 8 sed -i .bak -e 's/old word/new word/g'
like image 78
Bill Karwin Avatar answered Mar 27 '23 20:03

Bill Karwin

There is really only one way to speed it up and that is to make it execute sed less often, which you do by replacing the \; with +. This makes find group file names up to a convenient limit, roughly like xargs would do, but without the nuisances of having to rely on GNU -print0 and -0 options (to find and xargs respectively — though they are also available in the BSD find and xargs available on Mac OS X). It is also POSIX standard notation.

Note that you are making backups of your affected files with the suffix -e because the Mac version of sed insists on an argument for the -i option, and the -e happens to be there to be used, and the script does not have to be prefixed with -e.

Hence, this command should work faster:

find ./ -type f -exec sed -i .bak -e 's/old word/new word/g' {} +

How much faster is more debatable.

like image 33
Jonathan Leffler Avatar answered Mar 27 '23 21:03

Jonathan Leffler