UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 25th, 2005, 11:40 PM
sugarjulie sugarjulie is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 1 sugarjulie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 20 sec
Reputation Power: 0
Question recursive search & replace string in any file?

I'm just getting back into Unix after > 10 years away. I know I previously wrote a shell script that would recursively search through directories into each file for a particular string and replace it with something else. I just can't remember how to do this

I'm currently using ssh

Can anyone help me?

Reply With Quote
  #2  
Old May 26th, 2005, 09:26 AM
gruntz gruntz is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 53 gruntz User rank is Private First Class (20 - 50 Reputation Level)gruntz User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 22 h 39 m 25 sec
Reputation Power: 5
Assuming you have access to PERL, no need to write a script. You could use the "'PERL's of Wisdom", find and xargs. Like the following:

Code:
find . | xargs perl -p -i.bak -e 's/oldString/newString/;'


find . = will set up your recursive search. You can narrow your search to certain file by adding -name "*.ext" or limit buy using the same but add prune like -name "*.ext" -prune

xargs =sets it up like a command line for each file find finds and will invoke the next command which is perl.

perl = invoke perl
-p sets up a while loop
-i edit in place and the .bak will create a backup file like filename.ext.bak
-e execute the following....
's/ / /;' your basic substitute and replace.

just a suggestion....
Comments on this post
SimonGreenhill agrees!

Reply With Quote
  #3  
Old May 26th, 2005, 02:25 PM
deb_dutta deb_dutta is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 25 deb_dutta User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 2 m 4 sec
Reputation Power: 0
Thumbs up

Hi,
For recursive search u can used the following command
find . -print | xargs -i -t grep <enter search criteria> {}|more

for replacement purpose please use "sed" command.
See Man sed
Thanks
deb_dutta

Quote:
Originally Posted by sugarjulie
I'm just getting back into Unix after > 10 years away. I know I previously wrote a shell script that would recursively search through directories into each file for a particular string and replace it with something else. I just can't remember how to do this

I'm currently using ssh

Can anyone help me?

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > recursive search & replace string in any file?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway