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!
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
exactly what I had in mind (but didn't dig to deep into perl, which is more performant with File::Find ;-)
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.
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
for file in `grep -lr sun100 /some/dir`
do
sed 's/sun100/sun101/g' $file >/tmp/$$ && mv /tmp/$$ $file
done
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?
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?
ASKER
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
perl -i -pe "s/$search_string/$replace
needed to be
perl -i -pe "s/$search_string/$replace
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.
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
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`
Was not talking about this, but
perl -i -pe 's/sun100/sun101/g' `find . -type f`
find /path/to/dir -type f | xargs perl -i.bak -pe 's/sun100/sun101/g'