Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find and Replace in multiple files 1

Status
Not open for further replies.

DanAuber

IS-IT--Management
Apr 28, 2000
255
FR
I want to find the following text in about two hundred files (all in the same directory):

sysdate from dual);

and replace it with this line:

sysdate,max(osuser) from v$session where terminal = userenv('terminal'));

Is there a fast way to do this ? - I'm aware it may be more complicated due to the fact that I have quotation marks and a semi-colon in the text I want to bring in.

thanks for any help

dan
(HPUX 10.20)





Dan Auber
DanAuber@aol.com
 
Hello Dan,

Hope this one will help. I used a Bourne shell script and ex to solve the problem.

Create Replace.sh in your home directory

Insert:

#!/bin/sh
HomePath=/path/to/homedir
echo “Please enter the replacement directory path”
read DirPath
if [ -d $DirPath ]
then
echo “Please enter the old string:”
read OldString
if [ “$OldString” = “” ]
then
echo “No old string entered. Exit presumed.”
sleep 5
exit
fi
echo “Please enter the new string”
read NewString
echo “%s/$OldString/$NewString/g > $HomePath/Replace.ex
echo “x” >> $HomePath/Replace.ex
cd $DirPath
FileList=`grep –l ‘$OldString’ *`
for File in $FileList
do
ex –s $File < $HomePath/Replace.ex
done
cd $HomePath
else
echo “Bad Path Name. Exiting.”
sleep 5
fi
exit 0


Be sure to replace /path/to/homedir with your home directory path. Also make sure to change the permissions on the file to make it executable.
This script doesn't check to see if you entered a forward slash so to make it work, simply enter a backslash before the forward slash on input to get around this problem. The script should make a valuable mass replacement tool for any future use. To run the script simply enter the script name from the home directory.

./Replace.sh

The ./ assumes you are running as root and the current directory is not in the $PATH environment variable. (a bad old habit of mine)

Have fun!
Charlie Ehler
 
Hello Dan,

Hope this one will help. I used a Bourne shell script and ex to solve the problem.

Create Replace.sh in your home directory

Insert:

#!/bin/sh
HomePath=`pwd`
echo “Please enter the replacement directory path”
read DirPath
if [ -d $DirPath ]
then
echo “Please enter the old string:”
read OldString
if [ “$OldString” = “” ]
then
echo “No old string entered. Exit presumed.”
sleep 5
exit
fi
echo “Please enter the new string:”
read NewString
echo “%s/$OldString/$NewString/g > $HomePath/Replace.ex
echo “x” >> $HomePath/Replace.ex
cd $DirPath
FileList=`grep –l ‘$OldString’ *`
for File in $FileList
do
echo &quot;Processing $File&quot;
/bin/ex –s $File < $HomePath/Replace.ex
done
cd $HomePath
rm $HomePath/Replace.ex
else
echo “Bad Path Name. Exiting.”
sleep 5
fi
exit 0


Make sure to change the permissions on the file to make it executable. The Replace.ex file will be created in the working directory at the time the script was invoked, so if you execute the script from another location, the Replace.ex file will save in the directory you invoked it from.
This script doesn't check to see if you entered a forward slash so to make it work, simply enter a backslash before the forward slash on input to get around this problem. The script should make a valuable mass replacement tool for any future use. To run the script simply enter the script name from the home directory and answer the prompts.

./Replace.sh

The ./ assumes you are running as root and the current directory is not in the $PATH environment variable. (a bad old habit of mine)

Have fun!
Charlie Ehler
 
try this

perl -p -i -e 's/this/that/g' filename

Search and replace the string 'this' with the string 'that' in the file filename. You can also say * or *.html or any valid wildcard expression instead of filename. The s/// command uses regular expressions. If you want to alter the 'this' and 'that', it's best to avoid .*?[]{}$^ and other odd characters which act as metacharacters in regular expressions. Or better still look at the perlre documentation page. You can do this by saying perldoc perlre. Perl has extensive online documentation, try perldoc perltoc for a list.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top