LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 12-16-2004, 09:13 AM   #1
TroelsSmit
Member
 
Registered: Feb 2004
Location: Denmark
Distribution: Debian / Suse
Posts: 223

Rep: Reputation: 30
Search and Replace a string1 with string2 in many files


Hi,

I would like to do something like:

#!/bin/tcsh
$fil foreach( find . -name * ){
sed(%s/string1/string2/g)
}


that is,
1. go though a directory structure recursively.
2. replace string1 with string2 in all files

How do I do this ?

THANKS :-)
 
Old 12-16-2004, 09:31 AM   #2
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 46
For bash you would do something like

#!/bin/bash
for file in `find . -type f`
do
sed -i 's/string1/string2/g' $file;
done
 
Old 12-16-2004, 10:08 AM   #3
TroelsSmit
Member
 
Registered: Feb 2004
Location: Denmark
Distribution: Debian / Suse
Posts: 223

Original Poster
Rep: Reputation: 30
thanks,
got this scripts working:

#!/bin/bash
echo "Version 0"
rm -f tmp
for file in `find . -type f`
do
echo $file
sed 's/asdf/fdsa/g' $file > tmp;
rm -f $file
cat tmp > $file
rm -f tmp
done


(cannot be very fast, but it works)
 
Old 12-16-2004, 01:09 PM   #4
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 46
That does pretty much the same thing. You might want to use the -i option for sed which edits the file directly. Since you aren't doing anything with the tmp file anyways. Saves you a few lines.
 
Old 12-16-2004, 01:51 PM   #5
TroelsSmit
Member
 
Registered: Feb 2004
Location: Denmark
Distribution: Debian / Suse
Posts: 223

Original Poster
Rep: Reputation: 30
My version of SED doesn't know the "-i" option - thats why I rewrote your script.

Thanks again!
 
Old 12-16-2004, 02:04 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,004
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Update your sed to 4.x ;}

Besides that, if it wasn't for that deletion you could
even get away with

find -type f -exec sed -i 's/asdf/fdsa/g' {} \;


Cheers,
Tink

Last edited by Tinkster; 12-16-2004 at 02:18 PM.
 
  


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/replace in many files allelopath Linux - General 1 08-02-2005 10:21 PM
Search and Replace over multiple files The_Nerd Linux - Software 8 06-20-2004 07:59 AM
simple search and replace accross files/direcotries podollb Linux - Software 3 06-04-2004 09:30 PM
search for pattern in files and replace mizuki26 Linux - Newbie 3 01-04-2004 12:57 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 02:04 AM


All times are GMT -5. The time now is 10:47 AM.

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