Last active
November 17, 2015 19:41
-
-
Save MrCitron/7e2c6d3a0706c87fad3f to your computer and use it in GitHub Desktop.
Multiple search and replace in bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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