UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 7th, 2004, 03:10 PM
bjmazur2 bjmazur2 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 2 bjmazur2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unix find and replace text within all files within a directory

I am trying to do a search on an entire directory, finding all the instances of the text "applicationX" and replacing all instances with the text "applicationY".

I created the following to find all instances and print the files that contain the search text. But now I don't know how to replace the text.

find . -type f -exec grep "applicationX" {} \; -print

Please help..thanks

Reply With Quote
  #2  
Old May 8th, 2004, 10:34 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,536 Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 6 h 45 m 12 sec
Reputation Power: 876
Here's something I cooked up. The grep -il statement finds all instances of Hello in files ending in txt (i = ignore case, , l = only list the filename). The file names are passed to sed, which runs a regular expression to change all instances of Hello to Goodbye. Since sed doesn't overwrite a file, I redirected the output to a temp file and then renamed it back to the original file name.
Code:
#!/bin/sh
                          
for file in $(grep -il "Hello" *.txt)
do
sed -e "s/Hello/Goodbye/ig" $file > /tmp/tempfile.tmp
mv /tmp/tempfile.tmp $file
done
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Puzzle of the Month solved by Keath and KevinADC, superior perl programmers of the month
Looking for a perl job with kick-*** programmers in a well-known NASDAQ listed tech company with branches in the US and Europe? We're hiring. PM me for details. Requirements

Reply With Quote
  #3  
Old May 8th, 2004, 02:44 PM
christo's Avatar
christo christo is offline
Introspective
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Nov 2001
Location: London, UK
Posts: 3,297 christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 1 h 9 m 17 sec
Reputation Power: 104
Send a message via ICQ to christo Send a message via Yahoo to christo
or you could just aswell use PERLs inplace replacement as in the following one-liner:

find /path/to/start/from/ -type f | xargs perl -pi -e 's/applicationX/applicationY/g'

christo

Reply With Quote
  #4  
Old May 10th, 2004, 10:23 AM
bjmazur2 bjmazur2 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 2 bjmazur2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
The second solution worked perfectly.

The first one actually will delete all the contents of the file if there is more than one instance of the search string in the file. I actually had played around with another variation that did the same thing:

find . -type f -name '*.txt' -print | while read i
do
sed 's|applicationx|applicationy|g' $i > $i.tmp && mv $i.tmp $i
done

Maybe a little fiddling around would get a solution.

Reply With Quote
  #5  
Old June 6th, 2005, 10:22 AM
d-woo d-woo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 53 d-woo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 38 m 15 sec
Reputation Power: 7
To replace all instances of a string in a directory (subdirectories included) do:
Code:
perl -e "s/FIND/REPLACE/g;" -pi.save $(find path/to/DIRECTORY -type f)


The above will make a backup temp file of your original
If you do not want a temp file with the .save extension then do:

Code:
perl -e "s/FIND/REPLACE/g;" -pi $(find path/to/DIRECTORY -type f)


--------------------
Example:
You want to replace all instances of the word "design" with "dezine" in the directory /public_html/company/info

you can execute the command from document root as
Code:
perl -e "s/design/dezine/g;" -pi.save $(find public_html/company/info -type f)


or you can execute the command from public_html/company/ (a directory above) as:
Code:
perl -e "s/design/dezine/g;" -pi.save $(find info -type f)


------------------------------

The above commands will search all files (.gif, .jpg, .htm, .html, .txt) so you might see some error messages "Can't open *.gif", etc)

Simplified

To search just files of type, .htm without a backup file in the current directory only (no subdirectories) you could use:

Code:
perl -pi -e 's/design/dezine/g' *.htm

Reply With Quote
  #6  
Old March 6th, 2008, 03:56 PM
gare gare is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 1 gare User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 10 sec
Reputation Power: 0
Quote:
Originally Posted by Scorpions4ever
Here's something I cooked up. The grep -il statement finds all instances of Hello in files ending in txt (i = ignore case, , l = only list the filename). The file names are passed to sed, which runs a regular expression to change all instances of Hello to Goodbye. Since sed doesn't overwrite a file, I redirected the output to a temp file and then renamed it back to the original file name.
Code:
#!/bin/sh
                          
for file in $(grep -il "Hello" *.txt)
do
sed -e "s/Hello/Goodbye/ig" $file > /tmp/tempfile.tmp
mv /tmp/tempfile.tmp $file
done


very cool script. Thanks for posting. I modified it a bit, and this seems to work pretty well:
Code:
# *****************************************************************************************
# find_and_replace_in_files.sh
# This script does a recursive, case sensitive directory search and replace of files
# To make a case insensitive search replace, use the -i switch in the grep call
# uses a startdirectory parameter so that you can run it outside of specified directory - else this script will modify itself!
# *****************************************************************************************

!/bin/bash
# **************** Change Variables Here ************
startdirectory="/home/gare/tmp/tmp2"
searchterm="search"
replaceterm="replaceTerm"
# **********************************************************

echo "******************************************"
echo "* Search and Replace in Files Version .1 *"
echo "******************************************"

        for file in $(grep -l -R $searchterm $startdirectory)
          do
           sed -e "s/$searchterm/$replaceterm/ig" $file > /tmp/tempfile.tmp
           mv /tmp/tempfile.tmp $file
           echo "Modified: " $file
        done

echo " *** Yay! All Done! *** "

Reply With Quote
  #7  
Old June 28th, 2008, 06:42 PM
subverted subverted is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 3 subverted User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 4 m 30 sec
Reputation Power: 0
Script Modified for User Input

I modified the Scorpions4ever script so it allows for user input so that you dont have to alter the script to do a search.

edit: I changed the script to work with Zenity in Gnome.

Code:
#!/bin/sh

echo enter file name with extension
read NAME
echo enter word to replace
read FIND
echo enter word that replaces
read REPLACE                         
for file in $(grep -il "$FIND" $NAME)
do
sed -e "s/$FIND/$REPLACE/ig" $file > /tmp/tempfile.tmp
mv /tmp/tempfile.tmp $file
done


Only thing I can't figure out is how to run it from Nautilus Actions Configuration. I want it to be on the right click menu so that you can select multiple files and do batch replacements. I tried adding "gnome-terminal" to the beginning of the script but it doesn't work and my bash writing skills are minimal. I only know variable juggling.

I tried adding this line to the end before "done"

Code:
gnome-terminal -e /home/$USER/.gnome2/nautilus-scripts/scriptname


This actually popped up a terminal window and it seemingly went through the script operations but it didn't change the file I selected. Is there some way to name the file you select as a variable in the script?

I want it to be so that I can select multiple files with the mouse and right click with a simple "find and replace" function for batch editing, sort of like the batch-resize option that I added through Nautilus Actions Configuration Editor.

With Zenity, provides GUI question box:

Code:
#!/bin/sh

szAnswer0=$(zenity --entry --text "Enter file names and paths" --entry-text $PWD);
szAnswer1=$(zenity --entry --text "Enter word to replace:");
szAnswer2=$(zenity --entry --text "Enter word that replaces:");                     
for file in $(grep -il "$szAnswer1" $szAnswer0)
do
sed -e "s/$szAnswer1/$szAnswer2/ig" $file > /tmp/tempfile.tmp
mv /tmp/tempfile.tmp $file
done

Reply With Quote
  #8  
Old July 1st, 2008, 11:58 PM
ghostdog74 ghostdog74 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 99 ghostdog74 User rank is Lance Corporal (50 - 100 Reputation Level)ghostdog74 User rank is Lance Corporal (50 - 100 Reputation Level)ghostdog74 User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Day 19 h 49 m 58 sec
Reputation Power: 3
Code:
find . -type f -print0 | xargs -0 sed -i 's/Application/whatever/g'

Reply With Quote
  #9  
Old July 2nd, 2008, 01:15 AM
subverted subverted is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 3 subverted User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 4 m 30 sec
Reputation Power: 0
Nedit

yea, probably quicker to use NEDIT. It has a fantastic search and replace function, though a script to run a right click option on the mouse in nautilus is still interesting for search and replace.

Reply With Quote
  #10  
Old July 2nd, 2008, 05:56 PM
subverted subverted is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 3 subverted User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 4 m 30 sec
Reputation Power: 0
Thanks

Quote:
Originally Posted by ghostdog74
Code:
find . -type f -print0 | xargs -0 sed -i 's/Application/whatever/g'


Nice one. I changed it to a bash script like so:

Code:
#!/bin/bash
szAnswer1=$(zenity --entry --text "Enter word to replace:");
szAnswer2=$(zenity --entry --text "Enter word that replaces:");   
find . -type f -print0 | xargs -0 sed -i "s/$szAnswer1/$szAnswer2/g"


Then I moved the script file to /bin so it could be called as a single file name in terminal. Then I called it through Nautilus-Actions conf editor which added it to right click menu. Only problem is it doesn't work on desktop. It only shows up inside a directory, which is just as well, and it replaces the term for every file in the directory without calling a specific file.

Reply With Quote
  #11  
Old July 20th, 2008, 10:17 AM
onagar onagar is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 1 onagar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 25 sec
Reputation Power: 0
In that example - how to filter specific lines?

In this example:
find . -type f -print0 | xargs -0 sed -i 's/Application/whatever/g'

How can I filter that it will replace only lines starting with the string "#include"

I need to replace some string as above but only on lines starting with the "#include" string.

Thanks in advance,

Ofer

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Unix find and replace text within all files within a directory


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |