The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Go-Global for Unix V 2.2.4 ahm2002 UNIX for Dummies Questions & Answers 1 12-18-2007 11:41 AM
dynamic global script isingh786 Shell Programming and Scripting 2 01-24-2007 04:35 PM
Script to change UNIX password kornshellmaven Shell Programming and Scripting 12 05-17-2006 09:58 AM
Change password script in Unix easily.. zp523444 UNIX for Advanced & Expert Users 4 11-14-2005 11:14 AM
Urgent !!! - Script to Change passwords in unix tofani SUN Solaris 2 09-16-2003 03:58 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-01-2008
Registered User
 

Join Date: Feb 2008
Posts: 9
Stumble this Post!
Unix script to do a global change

I want to write a script to go into a unix directory and recursively change all the instances of a word in every file in every directory and sub directory.

Here's where I am now:

I can execute
find . -print | xargs grep "my_word"

to recuresively return all the files with my_word in it.

So, now I know which files contain my_word, and now I need to figure out how to write a script and go in and change every occurance of my_word to new_word.

Can anyone help?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-01-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,795
Stumble this Post!
sed:
Code:
find . -type f |\
while read file
do
      grep -q "my_word" && sed 's/my_word/new_word/g' $file > tmp.tmp && mv tmp.tmp $file
done
Reply With Quote
  #3 (permalink)  
Old 05-01-2008
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,002
Stumble this Post!
Quote:
Originally Posted by jim mcnamara View Post
sed:
Code:
find . -type f |\
while read file
do
      grep -q "my_word" && sed 's/my_word/new_word/g' $file > tmp.tmp && mv tmp.tmp $file
done
along the similar lines:
Code:
find . -type f |\
while read file
do
     { rm "${file}"; sed -e '/my_word/s/my_word/new_word/g' > "${file}"; } < "${file}"
done
Reply With Quote
  #4 (permalink)  
Old 05-01-2008
Registered User
 

Join Date: Apr 2008
Posts: 19
Stumble this Post!
gzip error

two diffeent files with same size after zipping giving different file size
Reply With Quote
  #5 (permalink)  
Old 05-01-2008
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,002
Stumble this Post!
Quote:
Originally Posted by sumitc View Post
two diffeent files with same size after zipping giving different file size
Pls don't hijack the threads of the others - start a new thread.
Reply With Quote
  #6 (permalink)  
Old 05-01-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,516
Stumble this Post!
With some versions of grep and xargs:

Code:
grep -FZlR old .|xargs -0 perl -i.bck -pe's/old/new/g'

Last edited by radoulov; 05-01-2008 at 02:23 PM.
Reply With Quote
  #7 (permalink)  
Old 05-12-2008
Registered User
 

Join Date: Feb 2008
Posts: 9
Stumble this Post!
I wanted to thank those who responded to my question. I ended up using a derivation of this solution:

-------------
find . -type f |\
while read file
do
{ rm "${file}"; sed -e '/my_word/s/my_word/new_word/g' > "${file}"; } < "${file}"
done
-----------
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:19 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0