Unix script to do a global change | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


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

Unix script to do a global change

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-01-2008
progkcp progkcp is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 5 March 2014, 2:43 PM EST
Posts: 18
Thanks: 0
Thanked 2 Times in 1 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?
Sponsored Links
    #2  
Old 05-01-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 19 December 2014, 4:53 PM EST
Location: NM
Posts: 10,312
Thanks: 297
Thanked 822 Times in 764 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
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 19 December 2014, 6:13 PM EST
Location: Foxborough, MA
Posts: 7,679
Thanks: 156
Thanked 591 Times in 555 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
sumitc sumitc is offline
Registered User
 
Join Date: Apr 2008
Last Activity: 2 August 2014, 8:49 AM EDT
Posts: 27
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
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 19 December 2014, 6:13 PM EST
Location: Foxborough, MA
Posts: 7,679
Thanks: 156
Thanked 591 Times in 555 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
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 19 December 2014, 9:35 AM EST
Location: Варна, България / Milano, Italia
Posts: 5,675
Thanks: 184
Thanked 620 Times in 578 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 06:23 PM..
Sponsored Links
    #7  
Old 05-12-2008
progkcp progkcp is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 5 March 2014, 2:43 PM EST
Posts: 18
Thanks: 0
Thanked 2 Times in 1 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
-----------
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 08:55 PM
Unix script to change password nimo Shell Programming and Scripting 7 12-17-2009 10:32 AM
Change the Windows Batch script to UNIX shell script. tomailraj Shell Programming and Scripting 5 08-27-2009 12:44 PM
Script to change UNIX password kornshellmaven Shell Programming and Scripting 12 05-17-2006 01:58 PM
Change password script in Unix easily.. zp523444 UNIX for Advanced & Expert Users 4 11-14-2005 03:14 PM



All times are GMT -4. The time now is 08:06 PM.