Page 2 -
Unix find and replace text within all files within a directory
Page 2 - 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.
Receive the tools necessary to be the rock star of your field. Our 12-month program teaches you the evolving world of multi-channel marketing as well as the complex issues and opportunities found in the industry.
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!
Web development can be a daunting task, even for specialists. There is a lot of information to absorb and a lot of technologies to learn in order to manage a superior website. When trying to learn the ropes, developers need a reliable source to introduce new ideas that can be easily implemented. When working on large projects, even web veterans may run into a technology or an aspect of a technology that they are unfamiliar with.
Posts: 1
Time spent in forums: 5 m 32 sec
Reputation Power: 0
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! *** "
This is an amazing script. Thanks so much for i. I spent over 2 hours looking for something to do exactly this. Going into my toolbox!! =)
Posts: 1
Time spent in forums: 14 m 53 sec
Reputation Power: 0
Perl uses a .cfg file which can't hold variables.
I have a shell script which executes one perl. The perl uses some variable which is again defined in a .cfg file. In that .cfg file I have a variable (LOG_FILE=$HOME/LOG). Since I can't use the $HOME inside the .cfg file which is used by perl, I have to each time change the .cfg file with value of $HOME, then in .cfg file I have LOG_FILE=/pkg/mt411c/LOG (as the value of $HOME=/pkg/mt411c). But everytime I copy this .cfg file to other environment I have to change the value of this LOG_FILE variable with the value of "$HOME" as it gets changed in dift env. But I want to make it generic so that I will not either change the value of $HOME inside the file or I want any other shell script which will change the value of these variables each time I put the .cfg file. (Note: I have several other variables in that .cfg file, this is just one example). Please help.
Posts: 1,917
Time spent in forums: 1 Month 5 Days 11 h 32 m 43 sec
Reputation Power: 1297
This really should have been a new thread ...
There must be a few ways to acheive this - can you not include the .cfg file in the perl, for example? Is theer now way of accessing the environment variables from within perl?
__________________
The moon on the one hand, the dawn on the other:
The moon is my sister, the dawn is my brother.
The moon on my left and the dawn on my right.
My brother, good morning: my sister, good night.
-- Hilaire Belloc