SUMMARY: Global Find/Replace Question...

From: Sun Consultant <sun_consultant_at_hotmail.com>
Date: Fri Jun 13 2003 - 14:16:58 EDT
Thanks to ALL the respondents - bmyster, Jason, Shaw, Stephan, Bertrand,
ttg, Santomauro, Eugene Schmidt (I used his suggestion), Dave Mitchell,
Andreas, Andy, Bischof, Jason Sanots, Darren Dunham, Bhavesh Shah, Crist
Clark, Jeremy Russell, Warren Brennan, Derek Olsen, Steve Maher...

The original question was:

> I want to search all the files in a directory for a particular word and
> replace all occurrences of that word with another...
> Is there a script I can use to do this, what would be the best way?

There were many many responses --

The two popular ways of doing this are Perl and Sed/Grep Script..

The one I used was the following script, which I could understand very
easily (Thanks to Eugene Schmidt):

    for FIL in `find /mydirectory -type f -exec grep -l "someword" {} \;`
    do
    cp $FIL $FIL.bu
    sed 's/someword/neword/g' $FIL.bu>$FIL
    done

I am summarizing the following responses that I received:

=========

try using perl..

perl -p -i -e 's/this/that/g' filename

Search and replace the string 'this' with the string 'that'
in the file filename. You can also say * or *.html or any valid
 wildcard expression instead of filename. The s/// command uses regular
 expressions.If you want to alter the 'this' and 'that', it's best to avoid
.*?[]{}$^
and other oddcharacters which act as metacharacters in regular expressions.
Or better
still look at the perl documentation page. You can do this by saying perldoc
perlre.
Perl has extensive online documentation, try perldoc perltoc for a list.

=========

#!/usr/bin/ksh

dir_name=$1
typeset -l l_string=$2
typeset -u u_string=$2
string_2=$3

for file in $( find $dir_name -type f | xargs grep -l -i "$2" )
do
print $file
sed "s/$l_string/$string_2/g;s/$u_string/$string_2/g" $file >
/tmp/tempfile.$$
mv /tmp/tempfile.$$ $file
done

=========

perl -i.bak -pe 's/REGEXP/REPLACEMENT/g' ./*

This will first make a backup of all files in the current directory with the
extension .bak and will replace (in place) REGEXP with REPLACEMENT in every
file. REGEXP can be any valid perl regular expression. (Very powerful)

If you don't have perl, I would suggest something like this in ksh:

for file in *
do
sed 's/REGEXP/REPLACEMENT/g' $file > $file.new
cp $file $file.old
mv $file.new $file
done

This would take all files in the current directory, replace REGEXP with
REPLACEMENT and place the changes into <filename>.new. It will then make a
backup of each file <filename>.old and rename <filename>.new to <filename>.
Note that REGEXP can be any valid sed regular expression.

=========

foreach i (`ls`)
   if -f $i then
      sed 's/particular word/another/g' $i > $i.tmp
      mv $i.tmp $i
   endif
end

=========

something like
for f in * ; do
      mv  $f $f.old
      sed 's/particular_word/another/' $f.old >$f
done

and after checking the result
rm *.old
you may need to use /[\t ]*particular_word[\t ]*/ to avoid substitutions if
particular_word is inside another word.

==========

    perl -i -pe 's/foo/bar/g' *

or if you want to copy each original file to file.bak at the same time,

    perl -i.bak -pe 's/foo/bar/g' *

==========

perl -pi -e "s=foo=bar=" *
perl -pi -e "s=foo=bar=g" *
                       ^- global, replace all occurrences of foo
  in all lines

==========

#!/usr/local/bin/ksh
#
TARGET_DIR=$1
SEARCH_STRING=$2
REPLACE_STRING=$3
SED=/usr/xpg4/bin/sed

for file in `ls $TARGET_DIR`; do
   $SED -e s/"\<$SEARCH_STRING\>"/$REPLACE_STRING/g $file > $file._sed
   mv $file $file.org
   mv $file._sed $file
done

===========
_______________________________________________
sunmanagers mailing list
sunmanagers@sunmanagers.org
http://www.sunmanagers.org/mailman/listinfo/sunmanagers
Received on Fri Jun 13 14:21:18 2003

This archive was generated by hypermail 2.1.8 : Thu Mar 03 2016 - 06:43:12 EST