Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
linux:search_and_replace_multiple_files_using_sed [2012/02/14 17:13]
Joel Dare
linux:search_and_replace_multiple_files_using_sed [2020/06/01 22:53] (current)
Line 1: Line 1:
 +====== Search and Replace Multiple Files using Sed ======
  
 +Here is a quick command that you can use to search and replace a piece of text across multiple files using find, xargs, and sed.
 +
 +  find . -name "​*.txt"​ -print | xargs sed -i "​s/​foo/​bar/​g"​
 +
 +===== Explanation =====
 +
 +  find . -name "​*.txt"​ -print
 +
 +This part of the command finds all of the files in the current directory that match the /*.txt/ pattern (in other words, all files that end with /.txt/.
 +
 +xargs
 +
 +This command is used to pass standard input to another command. It uses the single line output of the previous find command and executes the following sed command on each of the files.
 +
 +sed -i "​s/​foo/​bar/​g"​
 +
 +This command does an /in place/ search and replace (meaning it overwrites the existing file). ​ It uses the regular expression provided in quotes to do that replacement. ​ In this case, replacing /foo/ with /bar/.
 +
 +===== See Also =====
 +
 +If you found this useful you might also like the following articles.
 +
 +[[:Using AWK on CSV Files]]
 +
 +[[:Using AWK to Pull Specific Lines]]
 +
 +[[:​Technical Documents]]
comments powered by Disqus