DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Shell Script For Find And Replace

04.10.2012
| 103109 views |
  • submit to reddit
        Here's a little shell snippet for finding and replacing text in multiple files:

for fl in *.txt; do
mv $fl $fl.old
sed 's/find/replace/g' $fl.old > $fl
done
    

Comments

Snippets Manager replied on Sun, 2010/02/28 - 6:41pm

Just an FYI. Both the above posts seem tedious. You can do this in one line in a shell script or in your favorite CLI: (first cd /home/dir/of/choice **** make sure to check you have the correct directory with 'pwd' first!!!) Then: sed -i 's/search/replace/g' * SED is very powerful so please be careful. * means all files. you could also do *.py / *.pl / *.php -CHO AT FOUNDCO.COM

Snippets Manager replied on Mon, 2012/05/07 - 2:11pm

or, you could do: for i in `*.txt` do sed -i 's/find/replace/g' $i done