Operating System - Linux
1828456 Members
3662 Online
109978 Solutions
New Discussion

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.
5 REPLIES 5
Albert P Tobey
Occasional Advisor

Re: Find and replace text string script

find . -type f -exec perl -pi -e 's/serverA/serverB.domain.com/g' {} \;

All on one line, of course.
Lowell Lindeman
Occasional Advisor

Re: Find and replace text string script

Thank you for the input. I must be typing this in wrong or something because I receive the following error:

Malformed UTF-8 character (unexpected end of string) at -e line 1, <> line 6917.
Robert Salter
Respected Contributor
Solution

Re: Find and replace text string script

You could try this, it isn't a one liner, but what the hey.

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


Time to smoke and joke
Paddy_1
Valued Contributor

Re: Find and replace text string script

Typically I use the "tr" command to translate one string to another.Try man tr and use it in your loop for each file to accomplish the task.
The sufficiency of my merit is to know that my merit is NOT sufficient
Albert P Tobey
Occasional Advisor

Re: Find and replace text string script

Try:
export LC_ALL=C
find . -type f -exec perl -pi -e 's/serverA/serverB.domain.com/g' {} \;

Or the one-liner:
LC_ALL=C find . -type f -exec perl -pi -e 's/serverA/serverB.domain.com/g' {} \;

I'm guessing you're running a very new linux distribution like RH9 or Mandrake 9 - they have unicode supported in everything possible, including perl and the shell utils. This causes trouble with a bunch of stuff like this. Hint: running acrobat on RH9 works with the LC_ALL=C trick, too.