Link to home
Start Free TrialLog in
Avatar of theoradically
theoradically

asked on

Search and replace - ksh and perl

Sorry, I didn't have time to research this on my own.
I am looking for a ksh and perl script to go thru all files in all subdirectories of a directory
and do a search and replace for all instances of a specific server name.

go thru all files and search for sun100 and change it to sun101

Thanks for your help!
ASKER CERTIFIED SOLUTION
Avatar of wesly_chen
wesly_chen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'd go with a variation of wesly_chen's last simple suggestion:

find /path/to/dir -type f | xargs perl -i.bak -pe 's/sun100/sun101/g'
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
exactly what I had in mind (but didn't dig to deep into perl, which is more performant with File::Find ;-)
Avatar of NovaDenizen
NovaDenizen

The perl regexp 's/sun100/sun101/g' will match all occurances of sun100, including in terms like "datsun100" or "bosun10043".  It might be worthwhile to change the regexp to something like 's/\bsun100\b/sun101/' to exclude partial-word matches.  Keep in mind that perl considers all letters, numbers, and the '_' as word characters, so something like "sun100_blah" will be modified by the partial word match and ignored by the full word match.
Avatar of Tintin
If you have GNU grep, you could do:

for file in `grep -lr sun100 /some/dir`
do
   sed 's/sun100/sun101/g' $file >/tmp/$$ && mv /tmp/$$ $file
done
Avatar of theoradically

ASKER

I didn't get a chance to run Wesleys script until tonight.
When I run it, I get

$ ./search_and_replace.sh sun100 sun101
./search_and_replace.sh: ===: cannot open
./search_and_replace.sh: ===: cannot open
./search_and_replace.sh: ===: cannot open
./search_and_replace.sh: ===: cannot open
./search_and_replace.sh: ===: cannot open
./search_and_replace.sh: ===: cannot open
./search_and_replace.sh: ===: cannot open
./search_and_replace.sh: ===: cannot open
./search_and_replace.sh: ===: cannot open
./search_and_replace.sh: ===: cannot open

I checked the syntax of the script and have permissions on the files within the dir.
Can you offer any other suggestion?
As soon as I sent the last request, I found the error.

perl -i -pe "s/$search_string/$replace_string/g" $files   <=== use perl for faster result

needed to be

perl -i -pe "s/$search_string/$replace_string/g" $files      #  <=== use perl for faster result

Let me work with the perl variations a little and I'll award the points.  Thanks everyone
I'm glad to hear you find out the problem by your own.

I learned a lot from other experts here in EE.
Sometimes I prefer to write a longer but more comprehensive script so I can use it later on
for different pupose.

In the search_and_replace.sh,
perl -i -pe "s/$search_string/$replace_string/g" $files  
will NOT be susceptible to shell command line buffer problems since it is in "for" loop.
>perl -i -pe "s/$search_string/$replace_string/g" $files  

Was not talking about this, but
perl -i -pe 's/sun100/sun101/g' `find . -type f`