LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-24-2005, 05:52 AM   #1
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 408

Rep: Reputation: 31
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?
 
Old 07-24-2005, 08:17 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.

Last edited by druuna; 07-24-2005 at 08:19 AM.
 
1 members found this post helpful.
Old 07-24-2005, 05:03 PM   #3
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
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.
 
Old 07-24-2005, 05:33 PM   #4
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 408

Original Poster
Rep: Reputation: 31
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.
 
Old 07-25-2005, 12:15 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 07-25-2005, 04:12 AM   #6
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 408

Original Poster
Rep: Reputation: 31
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)
 
Old 07-30-2005, 04:09 PM   #7
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
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.
 
Old 10-17-2008, 03:00 PM   #8
fortezza
Member
 
Registered: Mar 2003
Location: Colorado
Distribution: Fedora Core 4
Posts: 297

Rep: Reputation: 30
Thumbs down

Quote:
Originally Posted by eddiebaby1023 View Post
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!
 
Old 10-17-2008, 03:27 PM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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).
 
Old 01-13-2010, 04:50 AM   #10
utw-mephisto
Member
 
Registered: Apr 2005
Posts: 93

Rep: Reputation: 16
Quote:
Originally Posted by druuna View Post
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
replace a substring with another string in C zeppelin Programming 21 11-09-2009 09:59 PM
Python: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 06:02 AM
How can I replace this string with another using sed? dave4545 Programming 7 01-27-2006 10:58 AM
[sed] replace string? chuanyung Programming 3 03-11-2004 08:42 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration