|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
Quote:
Code:
find . -type f |\
while read file
do
{ rm "${file}"; sed -e '/my_word/s/my_word/new_word/g' > "${file}"; } < "${file}"
done |
|
#4
|
|||
|
|||
|
gzip error
two diffeent files with same size after zipping giving different file size
|
| Sponsored Links | |
|
|
#5
|
||||
|
||||
|
Pls don't hijack the threads of the others - start a new thread.
|
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
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
|
|||
|
|||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|