Unix find and replace text within all files within a directory
Discuss Unix find and replace text within all files within a directory in the UNIX Help forum on Dev Shed. Unix find and replace text within all files within a directory UNIX Help forum discussing the Unix Operating System and all variants including Irix, Solarix, and AIX. Unix was designed as a true multi-user operating system.
The ASP Free website provides in-depth information on the latest developer tools available from Microsoft. Our cadre of writers, highly experienced industry experts, reveals the best ways to use established technologies as well as new and emerging technologies. Our coverage of Microsoft's development and administration technologies is among the most respected in the IT industry today.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month! Download and Activate to enter!
Intel® Graphics Performance Analyzers is a powerful tool suite for analyzing and optimizing your games, media, and graphics-intensive applications. Used by some of the best developers on the planet, Intel GPA lets you maximize your app’s performance.
Posts: 2
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
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 8,978
Time spent in forums: 1 Month 2 Weeks 6 Days 22 h 11 m 29 sec
Reputation Power: 3561
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 Diary of a first time dog owner <-- my cousin's blog
Posts: 2
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.
Posts: 1
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! *** "
Posts: 3
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.
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
Posts: 3
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.
Posts: 3
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.
Posts: 1
Time spent in forums: 11 m 38 sec
Reputation Power: 0
Hi Everyone. I just joined. what a wealth of info here. I was looking for something very similar to what these scripts do. I have a YaBB forum where I need to go into hundreds of *.vars files and search for a text string and if it finds it I want to replace the whole line not just the search string. Optionally it could report back a list of *.vars files that it did not find the search string in.
Posts: 1
Time spent in forums: 8 m 47 sec
Reputation Power: 0
A simple Perl example
Quote:
Originally Posted by bjmazur2
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
perl -pi -w -e 's/search/replace/g;' *.c
-p loop
-i means edit in-place
-w write warnings
-e means execute the following line of code.
Posts: 1
Time spent in forums: 2 m 31 sec
Reputation Power: 0
Remove ^M
Can you please modify this script to remove ^M from multiple files at once.
Quote:
Originally Posted by gare
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! *** "