The Student Asked: 2012-02-03 04:45:33 +0800 CST2012-02-03 04:45:33 +0800 CST 2012-02-03 04:45:33 +0800 CST How to "search and replace" many files? 772 I use the grep command to search for a string in many files. Is there something similar to "search and replace"? command-line files 4 Answers Voted Best Answer Jjed 2012-02-03T04:51:44+08:002012-02-03T04:51:44+08:00 You are looking for the sed command. For example, to replace the "dog" with "cat" in all text files in the current directory: sed -i 's/dog/cat/' *.txt Kerkdyk 2012-02-03T04:52:16+08:002012-02-03T04:52:16+08:00 sed comes to mind. Example: sed s/cat/dog/ <input >output This searches for cat in a line and puts dog on it's place in the file input and writes to file output. jcollado 2012-02-03T06:12:44+08:002012-02-03T06:12:44+08:00 sed is the right tool; but, as an alternative, you can even use ex commands in vim: vim -c 'args <files> | argdo %s/cat/dog/g | x' where <files> is the list of files or patterns in which you want to make the substitution. Zombo 2016-04-17T12:49:20+08:002016-04-17T12:49:20+08:00 You can use Vim in Ex mode: for b in *.txt do ex -sc '%s/OLD/NEW/g|x' "$b" done % select all lines s substitute g replace all instances in each line x save and close
You are looking for the
sed
command. For example, to replace the "dog" with "cat" in all text files in the current directory:sed comes to mind. Example:
This searches for cat in a line and puts dog on it's place in the file input and writes to file output.
sed
is the right tool; but, as an alternative, you can even useex
commands invim
:where
<files>
is the list of files or patterns in which you want to make the substitution.You can use Vim in Ex mode:
%
select all liness
substituteg
replace all instances in each linex
save and close