Wednesday, August 20, 2008

Shell script to find and replace strings

#!/bin/sh
#Script Find a string and replaces it with another string
if [ $# -eq 0 ]
then
echo
echo "Usage: find_replace.sh find_str replace_str ..."
echo
exit 1
fi
for files in $*; do
if [ "$files" = "$1" -o "$files" = "$2" ]
then
a=1
else
mv $files $files.old
sed 's/'$1'/'$2'/g' $files.old > $files
fi
done
rm *.old 2>/dev/null

1 comment: