LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 04-12-2007, 01:07 AM   #1
Abhilasha
LQ Newbie
 
Registered: Apr 2007
Posts: 4

Rep: Reputation: 0
Multiple string Replacement


Hi all,

Does any one know a command which will replace a particular string in all the files
in a directory in one shot ? thanks in advance ...

Abhilasha
 
Old 04-12-2007, 01:22 AM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You can use sed to change all of the strings in a file. Put your sed command in a for loop, such as:
Code:
for file in *.txt; do
  sed -i -f sedprogram "$file"
done
Make sure that you test the sed program thoroughly on a copy of a few of the files before running it on all of the files. For example, if you are replacing a phrase, it might be split between two lines. The before string might be a substring in places that you want to keep. If the first word of a phrase might begin a sentence, then the string and the replacement need to be capitalized.
 
Old 04-12-2007, 01:23 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

Something like this should work:

find . -type f -exec sed -i 's/oldstring/newstring/g' {} \;

The find command gets all files (type -f) in the current directory (.) and subdirs, all that is found is given to sed, sed changes oldstring in newstring.
Try the command without sed's -i option first. -i replaces it in-place.

This also takes care of 'special' file names (spaces etc).

See man sed and man find for details.

Hope this helps.
 
Old 04-12-2007, 01:42 AM   #4
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
For both of these examples, you may need to use 'xargs' if the number of files is too large.
 
Old 04-12-2007, 07:19 AM   #5
Abhilasha
LQ Newbie
 
Registered: Apr 2007
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks to both of you.. i tried the second option , and it worked ...
 
  


Reply

Tags
find


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
string padding and replacement functions George2 Programming 4 11-19-2006 02:09 AM
Shell Script - String Replacement revof11 Programming 7 11-29-2005 06:38 AM
replacement with sed: replace pattern with multiple lines Hcman Programming 5 11-18-2004 07:40 AM
Perl string replacement within an array? Seventh Programming 1 09-07-2004 02:50 PM
need to innitialize string variable with multiple spaces clsonnt Programming 3 08-11-2003 10:40 AM


All times are GMT -5. The time now is 03:28 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