Mail Archives: djgpp/1999/08/03/17:10:59
Dave Scott writes:
> Is there a simple utility available to do something like this :-
>
> edit <existing_string> <replacement_string> <a_bunch_of_filespecs>
A decent programming editor should have builtin support for
search/replace across multiple files. But if you need to do this
in a standalone form, try something like:
---- cut here, globalreplace.sh ----
#! /bin/sh
if [ $# -lt 2 ]; then
echo "Usage: globalreplace oldstring newstring files" 1>&2
exit 1
fi
oldstring=$1; shift
newstring=$1; shift
echo "Changing $oldstring to $newstring"
for file in $*; do
if [ -f $file ]; then
echo "Editing $file"
mv $file tmpfile
sed -e "s/$oldstring/$newstring/g" tmpfile > $file
touch -r tmpfile $file
rm tmpfile
else
echo "Error: $file not found"
fi
done
Shawn Hargreaves.
- Raw text -