Skip to content

Instantly share code, notes, and snippets.

@MrCitron
Last active November 17, 2015 19:41
Show Gist options
  • Save MrCitron/7e2c6d3a0706c87fad3f to your computer and use it in GitHub Desktop.
Save MrCitron/7e2c6d3a0706c87fad3f to your computer and use it in GitHub Desktop.
Multiple search and replace in bash
#!/bin/bash
# $1 : original file
# $2 : destination file
# $3 : patterns file
if [ ! $# -eq 3 ];
then
echo Usage : $0 original destination patterns
exit 1
fi
for i in $1 $3
do
if [ ! -f $i ];
then
echo File $i does not exist
exit 1
fi
done
tmp_file=$(mktemp)
cp $1 $tmp_file
OIFS=$IFS
IFS=","
while read old new
do
sed -i.bak "s/$old/$new/g" $tmp_file
done < $3
IFS=$OIFS
mv $tmp_file $2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment