Monday, August 24, 2009

Linux note: recursive replacement

This is a script to recursively do pattern replacing with dir

-----------------------------------
if [ $# -ne 3 ]
then
echo 'Usage: replace.sh dir pattern1 pattern2'
exit 85
fi

for f in `grep -rl $2 $1`;
do
sed "s/$2/$3/g" -i $f
done
--------------------------------------

$1 is the directory, $2 is the pattern to be replaced and $3 is the new pattern. This script iterates through all files in directory. Filtering can be done by changing the for loop into a detailed list of files.

No comments: