• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1102
  • Last Modified:

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!
0
theoradically
Asked:
theoradically
  • 2
  • 2
  • 2
  • +3
2 Solutions
 
wesly_chenCommented:
Check my post
http://www.experts-exchange.com/Operating_Systems/Solaris/Q_21221025.html
-- search_and_replace.sh ------
#!/bin/sh

script_name=`basename $0`
if [ $# -ne 2 ]
then
    echo "Syntax Error!"
    echo "Usage:    $script_name <search string> <replace string>"
    exit 1
fi

search_string=$1     # read first parameter
replace_string=$2    # read second parameter

rm -f /tmp/file_replaced   # clean the previous report file

for files in `find . -type f -exec file {} \; | grep -i text | awk -F: '{print $1}'`
do
   grep $search_string $files > /dev/null 2>&1
   if [ "$?" = "0" ]
   then
      echo $files >> /tmp/file_replaced        # report which file been replaced
      perl -i -pe "s/$search_string/$replace_string/g" $files   <=== use perl for faster result
   else
      continue
   fi
done
----

Then
chmod +x search_and_replace.sh
cd /path-to-dir
search_and_replace.sh  sun100  sun101

Or make it simple:
cd /path-to-dir
perl -i -pe 's/sun100/sun101/g' `find . -type f`
0
 
ahoffmannCommented:
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'
0
 
manav_mathurCommented:
perl -i -pe 's/sun100/sun101/g' `find . -type f`
is susceptible to shell command line buffer problems.

perl -MFile::Find -i.bak -e 'find(sub{-f&&{local @ARGV=("$File::Find::name");while(<>) {s/sun100/sun101/g;print}}},".")'

Manav
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ahoffmannCommented:
exactly what I had in mind (but didn't dig to deep into perl, which is more performant with File::Find ;-)
0
 
NovaDenizenCommented:
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.
0
 
TintinCommented:
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
0
 
theoradicallyAuthor Commented:
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?
0
 
theoradicallyAuthor Commented:
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
0
 
wesly_chenCommented:
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.
0
 
manav_mathurCommented:
>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`
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now