#!/usr/bin/sh # by bill mcnamara wimac@tidhom1g.grenoble.hp.com wmcn@ireland.com #set -x if [ $# -lt 3 ]; then echo "\n================================================================" echo " You must supply the old string and the new string:" echo " and the option" echo "" echo " Example: $0 oldtext newtext [preview | replace]" echo "" echo " CAUTION: using replace will overwrite the files" echo " containing the string $1" echo " NOTE: a backup of the file will be save in file.bak" echo " this file.bak will be deleted the next time" echo " this script is executed." echo "================================================================\n" exit 1 fi typeset OLD=$1 typeset NEW=$2 typeset OPT=$3 echo "\n=================================================================" echo "\nStarting sed" touch /tmp/sedname.log # cleanup for F in $(find . -type f | xargs grep $1 | awk '{print $1}' | cut -d: -f1) do if [ -f $F.bak* ]; then rm $F.bak* fi if [ -f $F.new* ]; then rm $F.new* fi # kludge for sed end of file 'bug' if [ -f .ranlast ]; then touch .ranlast else touch .ranlast chmod 600 .ranlast echo "" >> $F fi # don't want to look at binaries if [ `file $F|awk '{print $2}'` = ascii ] then echo ".\c" sed "s/$OLD/$NEW/g" $F > ${F}.bak if [[ $OPT = "replace" ]]; then echo "replace: $F" cp $F.bak $F fi fi # don't want to look at binaries if [ `file $F|awk '{print $3}'` = text ] then echo ".\c" sed "s/$OLD/$NEW/g" $F > ${F}.bak if [[ $OPT = "replace" ]]; then echo "replace: $F" mv $F.bak $F fi fi echo "$F" >> /tmp/sedname.log done # summarise echo "\nDone" echo "\n=================================================================" echo "\ninstances --- filename" cat /tmp/sedname.log | sort | uniq -c echo " -- " if [[ $? = 0 ]]; then echo " NOTE: $1 appears in the following filename/s" cat /tmp/sedname.log | grep $1 | uniq echo " rename the file/s if you find necessary" fi echo "\n=================================================================" rm /tmp/sedname.log #.end.