LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Find and replace string (https://www.linuxquestions.org/questions/programming-9/find-and-replace-string-346190/)

Johng 07-24-2005 05:52 AM

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?

druuna 07-24-2005 08:17 AM

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.

eddiebaby1023 07-24-2005 05:03 PM

Code:

perl -pi.bak -e 's/old/NEW/g'  *
will do the replacements, saving the original contents in a file with a .bak suffix. One command, not a find with an invocation of sed for every file.

Johng 07-24-2005 05:33 PM

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.

druuna 07-25-2005 12:15 AM

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.

Johng 07-25-2005 04:12 AM

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)

eddiebaby1023 07-30-2005 04:09 PM

Quote:

Originally posted by Johng
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.

Well, if you'd put the -e flag in as in my example, things may have been different.

fortezza 10-17-2008 03:00 PM

Quote:

Originally Posted by eddiebaby1023 (Post 1763347)
Code:

perl -pi.bak -e 's/old/NEW/g'  *
will do the replacements, saving the original contents in a file with a .bak suffix. One command, not a find with an invocation of sed for every file.

Yes, but Perl sux. Sed/awk is the way to go!

druuna 10-17-2008 03:27 PM

Quote:

Yes, but Perl sux. Sed/awk is the way to go!
@fortezza: Can I translate that to: I don't have any experience with perl. I do know (some) sed and awk, so that's what I'll use!

The perl solution is the more elegant of the 2 and probably faster (already stated by eddiebaby102).

utw-mephisto 01-13-2010 04:50 AM

Quote:

Originally Posted by druuna (Post 1762713)
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.

Hell of a bump but thanks for that .. was just searching for something like this and came across your post :D :D


All times are GMT -5. The time now is 09:22 PM.