The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 03:41 PM
dynamic global script isingh786 Shell Programming and Scripting 2 01-24-2007 08:35 PM
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 03:14 PM
Urgent !!! - Script to Change passwords in unix tofani SUN Solaris 2 09-16-2003 06:58 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-01-2008
progkcp progkcp is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 11
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?
  #2 (permalink)  
Old 05-01-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
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
  #3 (permalink)  
Old 05-01-2008
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
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 (permalink)  
Old 05-01-2008
sumitc sumitc is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 25
gzip error

two diffeent files with same size after zipping giving different file size
  #5 (permalink)  
Old 05-01-2008
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
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.
  #6 (permalink)  
Old 05-01-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
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..
  #7 (permalink)  
Old 05-12-2008
progkcp progkcp is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 11
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

Bookmarks

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:59 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0