Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Replacing string in multiple files

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 09-10-2009
arulanandsp arulanandsp is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 10 September 2009, 11:27 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Replacing string in multiple files

Hi,

I need to replace the string 'abcd' with 'xyz' in a file sample.xml
This sample.xml is also present in the subdirectories of the current directory.

Eg,

If I am in /user/home/

the sample.xml if present in
/user/home/
/user/home/folder1/
/user/home/folder2/
/user/home/folder2/subfolder1/

I tried using
sed "s/abcd/xyz/g" sample.xml > temp; mv temp sample.xml;

But this replaces only the sample.xml in /user/home/ directory.

Please help on this
Sponsored Links
    #2  
Old Unix and Linux 09-10-2009
girish1428 girish1428 is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 16 May 2013, 3:40 PM EDT
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Please test and try this....



Code:
 
for i in `find /user/home/ -type f`
do
sed "s/abcd/xyz/g" $i > temp; mv temp $i;
done

Sponsored Links
    #3  
Old Unix and Linux 09-10-2009
arulanandsp arulanandsp is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 10 September 2009, 11:27 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,

I modified little bit and this worked.

Code:
for y in `find . -name sample.xml`;
do 
sed 's/abcd/xyz/g' $y > temp; mv temp $y;
done

Thanks for your help
    #4  
Old Unix and Linux 09-11-2009
thegeek thegeek is offline
Read Only
 
Join Date: Apr 2009
Last Activity: 30 July 2012, 5:38 AM EDT
Location: /usr/bin/vim
Posts: 946
Thanks: 13
Thanked 38 Times in 36 Posts
Another variation


Code:
find ~ -name "sample.xml" -exec sed -i 's/abcd/xyz/' {} \;

Warning: But be very careful while running all these, as it will recursively edit all the sample.xml file in your home directory.
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
renaming multiple files while replacing string kevin9 Shell Programming and Scripting 2 05-10-2009 06:41 PM
Multiple search string in multiple files using awk pinnacle Shell Programming and Scripting 0 05-05-2009 12:08 PM
Replacing string in files mohan705 Shell Programming and Scripting 2 08-29-2007 01:27 AM
replacing a string in all files in a dir chiru_h Shell Programming and Scripting 3 07-25-2006 04:40 PM
String replacement in multiple files WABonnett Shell Programming and Scripting 2 02-17-2004 02:49 PM



All times are GMT -4. The time now is 10:12 AM.