Operating System - HP-UX
1820619 Members
1860 Online
109626 Solutions
New Discussion юеВ

Trying this agian... Find and replace text string script.

 
SOLVED
Go to solution
Lowell Lindeman
Occasional Advisor

Trying this agian... 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... so I will try and break it down even further.

I have copied a directory to my local machine (not testing with production data)
I need to search for a string of text, that being 'serverA' and change it to 'serverB'. Now I know for a fact that this sting of text will be located in 100's of text files and even be part of directory names. This string of text 'serverA' is located in literally hundreds of places with in this directory and all of the sub-directories under it. When the string of text "serverA" is located I need to replace that string of text "serverA" with the new sting of text "serverB". So in short I need to replace 'serverA' which is located both inside files and directory names.

I thought someone provided the answer on my previous post but after further investigation none of the text strings were replaced.

Thank you again for all of your help.
18 REPLIES 18
Steven E. Protter
Exalted Contributor

Re: Trying this agian... Find and replace text string script.

I'm attaching a script that works for this.

Its a perl script.

It scans for text, creates a filelist and makes the change.

Hope it helps.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Pete Randall
Outstanding Contributor

Re: Trying this agian... Find and replace text string script.

Lowell,

I think you did get good answers in the previous thread, but I would suggest you break this down into two passes: one for files using "find -type f" and one for directories using "find -type d" as the processing requirements will be different.


Pete



Pete
Bryan D. Quinn
Respected Contributor

Re: Trying this agian... Find and replace text string script.

Hey Lowell,

The following will work for making changes within a file that you have open with vi.

:g/serverA/s//serverB/g

This will make a global change of serverA to serverB, inside of a file...using vi.

-Bryan
William Wong_2
Trusted Contributor

Re: Trying this agian... Find and replace text string script.

You need to break down the problem into it's 2 components. Finding the files with the text string you need replace and then editing and
replacing the text string in those files. The
below command should work:

grep -l serverA * | sed "s/serverA/serverB/g




Tim D Fulford
Honored Contributor

Re: Trying this agian... Find and replace text string script.

To further expand Brians answer

#!/usr/bin/ksh
for file in $(ls -R)
do
if [-f $file]
then
vi $file << EOF
:1,$ s/serverA/serverB/g
:wq!
EOF
elif [-d $file]
then
newfile=$(echo $file ex "s/serverA/serverB/g")
mv $file $newfile
fi
done

This will EDIT the files & move the directories. I have not tested it, so you may need to tweek.

Regards

Tim
-
Rodney Hills
Honored Contributor

Re: Trying this agian... Find and replace text string script.

If you use any of the perl script examples, be sure to download version 5.x. The default one under /usr/contrib/bin is version 4.x and is missing a lot of functionality.

You haven't mentioned why none of the solutions are working. Some feedback could help the community give you a solution.

-- Rod Hills
There be dragons...
William Wong_2
Trusted Contributor

Re: Trying this agian... Find and replace text string script.

Whoops actually I skipped one piece and flipped the order I verified that the following command worked:

sed "s/serverA/serverB/g" `find . -type f -print | xargs grep -l serverA`

That will find all the files from the directory you start the command from with the text string serverA and replace it with serverB.
Lowell Lindeman
Occasional Advisor

Re: Trying this agian... Find and replace text string script.

this script throws off the following output:

sh [3]: [-f: not found.
sh [9]: [-d: not found.

#!/usr/bin/ksh
for file in $(ls -R)
do
if [-f $file]
then
vi $file << EOF
:1,$ s/serverA/serverB/g
:wq!
EOF
elif [-d $file]
then
newfile=$(echo $file ex "s/serverA/serverB/g")
mv $file $newfile
fi
done
Lowell Lindeman
Occasional Advisor

Re: Trying this agian... Find and replace text string script.

Willaim,

Your script is running but it is not changing any of the files or directory names.

Just trying to provide feed-back.

Thanks again.
Robert Salter
Respected Contributor

Re: Trying this agian... Find and replace text string script.

You asked this same thing in another thread, right? I'll repost my idea here too.

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


The first sed gets rid of the "./" from the find, assuming you're in the top of the directory you want to do the mods in.

As for changing the directory names, it pretty much the same.

for d in `find . -name *serverA* -type d -exec ls -d {} \; | sed 's/\.\///'`
do
mv $d $d.com
done



Time to smoke and joke
Robert Salter
Respected Contributor

Re: Trying this agian... Find and replace text string script.

I'm wrong on the directory name change, I'm taking too much for granted as to how your directories are named. Maybe I should just keep to reading and not trying to answer.





Time to smoke and joke
Rodney Hills
Honored Contributor

Re: Trying this agian... Find and replace text string script.

Williams script is not updating the original files because "sed" reads from stdin (can be multiple files) and writes ONE stdout file.

You can use "sed" as long as you iterate over each filename, have sed input each file one at a time, output the result to one file, and then replace the original file with the newly created output file.

A more efficient "find" statement would be-

find . -print | xargs grep -l

You original "find" calls grep for each filename. By using xargs you can call "grep" fewer times and reduce overhead and time.

HTH

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: Trying this agian... Find and replace text string script.

Williams script is not updating the original files because "sed" reads from stdin (can be multiple files) and writes ONE stdout file.

You can use "sed" as long as you iterate over each filename, have sed input each file one at a time, output the result to one file, and then replace the original file with the newly created output file.

A more efficient "find" statement would be-

find . -print | xargs grep -l "ServberA"

You original "find" calls grep for each filename. By using xargs you can call "grep" fewer times and reduce overhead and time.

HTH

-- Rod Hills
There be dragons...
Lowell Lindeman
Occasional Advisor

Re: Trying this agian... Find and replace text string script.

Hello Rodney,

Thank you for the update... but I'm lost... can you dumb it down for me a bit and give me more detail?

Thank you.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Trying this agian... Find and replace text string script.

Hi:

This is one way:

#!/usr/bin/sh
FILE=/tmp/results
cd /tmp/dummydir #...change this...
find . -type f -print|xargs grep -l serverA > ${FILE}
while read INPF X
do
sed -e 's/serverA/serverB/g' ${INPF} > ${INPF}.new
[ -f "${INPF}.new" ] && mv ${INPF}.new ${INPF}
done < ${FILE}
rm ${FILE}
exit 0

Regards!

...JRF...
Robert Salter
Respected Contributor

Re: Trying this agian... Find and replace text string script.

OK, so I got wrapped in this, so try this.

for i in `find . -name *serverA* -type d -exec ls -d {} \;`
do
echo $i
newdir=`echo $i |sed 's/serverA/serverB/' `
mv $i $newdir
done


make a backup before trying anyhting.


Time to smoke and joke
Tim D Fulford
Honored Contributor

Re: Trying this agian... Find and replace text string script.

Way back up the thread I provided a script (that did not work!!)

the "if [ -f $file ]" means if $file is a file, and "elif [ -d $file ]" mean else if $file is a directory. They SHOULD work if not try man ksh & look for the relavent section. Currently I'm @home & do not have a computer to try this out on (sorry :-( )

Alternatively there are alot of replies using find (an fine solution, probably more efficient too)

Tim
-
Rodney Hills
Honored Contributor

Re: Trying this agian... Find and replace text string script.

James example is what I was talking about. He iterates over each of the filenames. Executes sed on each one generating a new file. Moves the new file into the original.

Unix shell programming can be frustrating at first, but with experience it can do some fantastic things (and perl even more so)!

-- Rod Hills
There be dragons...