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


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 !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-01-2008
Registered User
 
Join Date: Feb 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
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?
Sponsored Links
    #2  
Old 05-01-2008
...@...
 
Join Date: Feb 2004
Location: NM
Posts: 9,738
Thanks: 179
Thanked 662 Times in 638 Posts
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

Sponsored Links
    #3  
Old 05-01-2008
vgersh99's Avatar
ɹoʇɐɹǝpoɯ
 
Join Date: Feb 2005
Location: Foxborough, MA
Posts: 7,384
Thanks: 112
Thanked 486 Times in 458 Posts
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

    #4  
Old 05-01-2008
Registered User
 
Join Date: Apr 2008
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
gzip error

two diffeent files with same size after zipping giving different file size
Sponsored Links
    #5  
Old 05-01-2008
vgersh99's Avatar
ɹoʇɐɹǝpoɯ
 
Join Date: Feb 2005
Location: Foxborough, MA
Posts: 7,384
Thanks: 112
Thanked 486 Times in 458 Posts
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.
Sponsored Links
    #6  
Old 05-01-2008
radoulov's Avatar
--
 
Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 5,481
Thanks: 144
Thanked 541 Times in 509 Posts
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 05:23 PM..
Sponsored Links
    #7  
Old 05-12-2008
Registered User
 
Join Date: Feb 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
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
-----------
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Setting a permanent global variable in unix accessible from any script arindamlive UNIX for Advanced & Expert Users 3 09-06-2011 07:55 PM
Unix script to change password nimo Shell Programming and Scripting 7 12-17-2009 09:32 AM
Change the Windows Batch script to UNIX shell script. tomailraj Shell Programming and Scripting 5 08-27-2009 11:44 AM
Script to change UNIX password kornshellmaven Shell Programming and Scripting 12 05-17-2006 12:58 PM
Change password script in Unix easily.. zp523444 UNIX for Advanced & Expert Users 4 11-14-2005 02:14 PM



All times are GMT -4. The time now is 08:30 AM.