Operating System - HP-UX
1832961 Members
3160 Online
110048 Solutions
New Discussion

Re: Find and replace text string script

 
SOLVED
Go to solution
Lowell Lindeman
Occasional Advisor

Find and replace text string script

Hello All,

I could really use some help with a task I have been working on. I'm in the process of moving an application from one server to another. Part of this process involves me searching a directory and all sub-directories below it for text files containing the old server name and replacing that with a fully qualified DNS name.

I have been searching and reading MAN pages for a day now but have not been able to gain any ground. I have come up with a command to find all the files that need to be changed but I???m at a loss for the command to replace the text string.

# find . ???depth ???exec grep ???l ???serverA??? {} \;

Any help would greatly be appreciated.
9 REPLIES 9
John Payne_2
Honored Contributor

Re: Find and replace text string script

What about:

find . -name oldhostname -print -exec mv oldhostname newhostname.your.domain {} \;

To just find them:

find . -name oldhostname -print

Hope it helps

John
Spoon!!!!
John Payne_2
Honored Contributor

Re: Find and replace text string script

Oops... that won't work. I'll get back to you in a second...

You could do a
find . -name oldhostname -print > jjp.out

Then edit the file to add the mv command wrapped around the file names...

John
Spoon!!!!
Massimo Bianchi
Honored Contributor

Re: Find and replace text string script

find . ???depth ???exec grep ???l ???serverA??? {} \; -print | while read FILENAME
do
echo Saving old file $FILENAME
cp -p $FILENAME $FILENAME.orig
echo Changing old to new
cat $FILENAME.orig | sed s.OLDHOST.NEWHOST.g > $FILENAME
done


Patrick Wallek
Honored Contributor

Re: Find and replace text string script

Is it something within the file that has to be changed or is it the filename itself?

If it is something within the file, you can use your find statement and send the resulting files that will need to be modified to a file.

# find n. -depth -exec grep -i 'serverA' {} \; > /tmp/filename

Then you can work from /tmp/filename and use sed to search thru each file and do your substitution.

Something like:

# for i in $(cat /tmp/filename)
do
sed with some options against $i
done

James R. Ferguson
Acclaimed Contributor

Re: Find and replace text string script

Hi Lowell:

You could do something like:

#!/usr/bin/sh
FILE=/tmp/results
cd /tmp/dummydir
find . -print |xargs grep hi > ${FILE}
OLDIFS=${IFS}
IFS=:
while read INPF X
do
sed -e 's/hi/bye/g' ${INPF} > ${INPF}.new
[ -f "${INPF}.new" ] && mv ${INPF}.new ${INPF}
done < ${FILE}
exit 0

In this example, I have searched for "hi" and replaced "hi" by "bye".

Regards!

...JRF...
William Wong_2
Trusted Contributor
Solution

Re: Find and replace text string script

Actually the easiest approach is since you have found all the files just pass the file name to sed and have sed replace the text in those files. So add pipe to :
sed -e 's/serverA/serverB /g'

Where serverA is the old server name and
serverB is the new server name.
Robert Salter
Respected Contributor

Re: Find and replace text string script

You can try this;


for i in `find . -depth -exec grep -l 'serverA' {} \; | sed 's/\.\///'`
do
sed 's/serverA/serverA.ur.domain/' $i > $i.m
mv $i.m $i
done

I'd make copies of the files first, you just can't be too sure.

Bob
Time to smoke and joke
Lowell Lindeman
Occasional Advisor

Re: Find and replace text string script

Ok... I'm really new to this and want to make sure I'm being clear with what I'm trying to do.

I have made copied a directory to my local machine (not working with production data)
I have to search each text file, and only text files, located in this directory and every sub-directory within this directory for a string of text "serverA". When the string of text "serverA" is located I need to replace that string of text "serverA" with the new sting of text "serverB".

There are literally 100's of text files located in this directory... So I'm trying to put a script together to accomplish this task... another thing I need to keep in mind is that disk space is limited in production.

Thank you in advance.
Rodney Hills
Honored Contributor

Re: Find and replace text string script

You've got the first part with the find. That will get you the file paths of all files that contain the text you are looking for. Pass those file name to a perl to do the text change-

For instance-
perl -p -i -e 's/ServerA/ServerA.xyz.com/g' `find . ???depth ???exec grep ???l ???serverA??? {} \;`

Putting back-quotes (`) around the find will be the results onto the command line. perl then will apply the change through the entire list of filenames and save the changed file back into the original filename.

Note- A backup of the folder would be a prudent idea...

Note- If you have too many file names it will exceede the shells maximum command line length. In that case you could use-
find . ???depth ???exec grep ???l ???serverA??? {} \; | xargs perl -p -i -e 's/ServerA/ServerA.xyz.com/g'

Do a man on xargs, it is handy for breaking up command lines into managable blocks.

HTH

-- Rod Hills
There be dragons...