If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > find and replace in unix

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-23-04, 13:13
saccskiz saccskiz is offline
Registered User
 
Join Date: Feb 2004
Posts: 143
find and replace in unix

Hi,
Does anyone have a unix script that will find a string and replace it with an other string in all the files right under the directory from where this is run...and also all the files in the subdirectories. For example, I would like to replace the word mydb1 to mydb2 in all .sh files and subdirectory files, how do i get this done? Any suggestions on this would be greatly appreciated. Thanks
Reply With Quote
  #2 (permalink)  
Old 02-23-04, 14:02
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Try this :

Code:
find . -name '*.sh' | xargs perl -p -i -e 's/mydb1/mydb2/g'
If you want to keep a save (*.sh_save) of your files :

Code:
find . -name '*.sh' | xargs perl -p -i'_save' -e 's/mydb1/mydb2/g'
__________________
Jean-Pierre.
Reply With Quote
  #3 (permalink)  
Old 02-23-04, 14:40
saccskiz saccskiz is offline
Registered User
 
Join Date: Feb 2004
Posts: 143
Thank you Aigles. Worked perfect. If you can help me tweak this a little further, that would be wonderful.

Keeping the same thing in place ,
( find . -name '*.sh' | xargs perl -p -i'_save' -e 's/mydb1/mydb2/g' )


suppose, in place of mydb1, i want to include a string with slashes -
something like /usr/limo/bin and replace it with /usr/limo9i/bin,
how do i do this ? The reason I am doing this is because there are directory paths included within scripts and I would prefer to provide the whole path so that I don't replace anything that is unintended. Thanks a lot
Reply With Quote
  #4 (permalink)  
Old 02-23-04, 15:43
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Code:
find . -name '*.sh' | xargs perl -p -i'_save' -e 's+/usr/limo/bin+/usr/limo9i/bin+g'
You can use any character in place of / (+ in this example) like you can do with sed.
__________________
Jean-Pierre.
Reply With Quote
  #5 (permalink)  
Old 02-23-04, 15:51
saccskiz saccskiz is offline
Registered User
 
Join Date: Feb 2004
Posts: 143
Thanks a bunch
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On