Find and replace string
Can someone give me a command to find a sting in all the files in a directory, and replace it with a different string?
|
Hi,
This should work (only if sed 4.+ is used Mandrake 9.2 does): find /tmp -name "*tst" -exec sed -i 's/old/NEW/g' {} \; Here's a little breakdown of the above command: Find searches in /tmp and lookes for files ending with tst (-name "*tst"), found files are given to sed (-exec sed -i 's/old/NEW/g' {} \; ). Sed replaces the string (old in the above example) with NEW in the found file. This is possible due to the -i option in sed version 4.+ Hope this helps. |
Code:
perl -pi.bak -e 's/old/NEW/g' * |
Thank you druuna, I am using Mandriva 2005, with sed version 4.1.1, and I am trying to mass edit a collection of html files in a directory.
I tried your command, as below, but got an error message: $ find /Wm -name "*" -exec sed -i 's/"Times New Roman","Times","serif"/"Helvetica","Arial","sans-serif"/g'{}\; find: missing argument to `-exec' And thank you eddiebaby1023, when I tried your command as below, I got: perl -pi.bak 's/"Times New Roman","Times","serif"/"Helvetica","Arial","sans-serif"/g' * Can't open perl script "s/"Times New Roman","Times","serif"/"Helvetica","Arial","sans-serif"/g": No such file or directory. Use -S to search $PATH for it. |
Hi,
find is giving you an error due to the lack of a space between the g' and {}. Is this sed statement correct, I doubt it. Hope this helps. |
Thank you druuma, cool.
This one worked: find temp -name "*.html" -exec sed -i 's/"Times New Roman","Times","serif"/"Helvetica","Arial","sans-serif"/g' {} \; note the lack of a "/" in front of the directory name (it would not work with the slash) |
Quote:
|
Quote:
|
Quote:
The perl solution is the more elegant of the 2 and probably faster (already stated by eddiebaby102). |
Quote:
|
All times are GMT -5. The time now is 09:22 PM. |