LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 06-02-2004, 06:23 PM   #1
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Search and Replace over multiple files


How can I do a search and replace over multiple files in different directories??? Example:

find . -name "*.*" -exec (find replace program???) {} "string1" "string2" \;

Something like that....

Thanks in advance.
 
Old 06-02-2004, 11:01 PM   #2
darthtux
Senior Member
 
Registered: Dec 2001
Location: 35.7480 N, 95.3690 W
Distribution: Debian, Gentoo, Red Hat, Solaris
Posts: 2,070

Rep: Reputation: 45
I haven't tried this using multiple directories but works great on what I've used it for

perl -i.DontBlameStW -pe "s/current_string/string_to_replace_with/g" files

I think to use something with find, you will probably need a sed script. Maybe try the code below out and let me know if it works.

find . -type f -exec perl -i.DontBlameStW -pe "s/current_string/string_to_replace_with/g" {} \;
 
Old 06-03-2004, 04:48 AM   #3
kbcnetau
Member
 
Registered: Dec 2003
Location: South Australia (ex-Devon, UK)
Distribution: SuSE, Slackware, Fedora, Debian, Knoppix
Posts: 141

Rep: Reputation: 15
The problem that you'll have using find is that it will try to feed the directories to the programme doing the substitution, as well as the files, causing it to bomb. (I just wrote a little shell script to test this and that's what happened.)

I will consider this further and report back if I find a solution - it's a "real world" problem that must have a simple answer!
 
Old 06-03-2004, 05:24 AM   #4
kbcnetau
Member
 
Registered: Dec 2003
Location: South Australia (ex-Devon, UK)
Distribution: SuSE, Slackware, Fedora, Debian, Knoppix
Posts: 141

Rep: Reputation: 15
Got it!

I set up a shell script called 'mysubs':

cp $1 $1.old;
cat $1 | sed s/string_1/string_2/g > $1;

The cp is to make a safety copy before we overwrite the file...

Then:

find -type f -exec mysubs {} \;

The -f flag makes sure that we only handle regular files.

Note - if mysubs is in the directory we're searching, the string_1 in the script will also be substituted!
 
Old 06-03-2004, 06:09 PM   #5
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Original Poster
Rep: Reputation: 32
Sweet dood. Thanks!... I just don't understand the one argument to sed, could you explain???

s/string_1/string_2/g

??//???
Thanks again!

Btw, couldn't I do the following and have the same result (without backup that is)?

find . -f -exec cat {} | sed s/string_1/string2/g > {} \;

????

Last edited by The_Nerd; 06-03-2004 at 06:12 PM.
 
Old 06-03-2004, 06:30 PM   #6
kbcnetau
Member
 
Registered: Dec 2003
Location: South Australia (ex-Devon, UK)
Distribution: SuSE, Slackware, Fedora, Debian, Knoppix
Posts: 141

Rep: Reputation: 15
Quick refresher on sed:

s/string_1/string_2/g

* the first s is substitue (someone correct me if I'm wrong!)
* between the first and second slashes is the string you want changed
* between the second and third slashes is what you want the string changed to
* the g means do all occurences, not just the first one (g=global)

Note - if you want to have / or other reserved characters in your string(s), you will need to escape it with a backslash \.

Not sure on your last example - do what I did with mine and try it!

I just set up a temporary directory with a file in it containing the the string I wanted to change and a subdirectory with a further file also containing that string. Then I tweaked and ran the code until it worked, making many references to the find manpage in the process...

Conclusion:
1) experiment - just not on live data ;-)
2) manpages Are Your Friends.
 
Old 06-04-2004, 06:22 PM   #7
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Original Poster
Rep: Reputation: 32
Thanks again!
 
Old 06-20-2004, 04:16 AM   #8
alexmacfie
LQ Newbie
 
Registered: Jun 2004
Location: Kingston upon Thames
Distribution: OpenSUSE
Posts: 2

Rep: Reputation: 0
Quote:
Originally posted by kbcnetau
Got it!

I set up a shell script called 'mysubs':

cp $1 $1.old;
cat $1 | sed s/string_1/string_2/g > $1;

I found that it truncates the new files to 8192 bytes.

To correct this cat the backup file instead of the original, so line 2 becomes:
Code:
cat $1.old | sed s/string_1/string_2/g > $1;
Alex
 
Old 06-20-2004, 06:59 AM   #9
kbcnetau
Member
 
Registered: Dec 2003
Location: South Australia (ex-Devon, UK)
Distribution: SuSE, Slackware, Fedora, Debian, Knoppix
Posts: 141

Rep: Reputation: 15
I think that the trucation issue is a case of "your mileage may vary" (it's a buffering thing, I think), but Alex's solution is actually better practice than my original suggestion.

As a rule, it's probably best NOT to take the contents of a file, process it and then put it back in - all in one step. If your process doesn't involve taking a copy, just use a temporary file and delete it afterwards.

Cheers

Matthew
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Search and Replace with multiple-line strings ChristianNerds.com Programming 4 08-21-2005 02:32 PM
search/replace in many files allelopath Linux - General 1 08-02-2005 09:21 PM
Looking for a multiple file search and replace tool, prefer graphical haimeltjnfg Linux - Software 6 02-02-2005 10:53 PM
search for pattern in files and replace mizuki26 Linux - Newbie 3 01-04-2004 11:57 AM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 01:56 PM


All times are GMT -5. The time now is 01:37 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration