Created
April 13, 2020 12:15
-
-
Save mvogelgesang/1e5b30f1619807b77c2ca673f4eba0e2 to your computer and use it in GitHub Desktop.
Shell script to loop through a CSV file containing a series of values to find and replace
This file contains 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
newterm1 | oldterm1 | |
---|---|---|
newterm2 | oldterm2 | |
newterm3 | oldterm3 |
This file contains 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 | |
# set -x | |
# CSV FILE MUST HAVE EMPTY LINE AT END AND NO "" | |
INPUT=inputdata.csv | |
# to exclude multiple directories, wrap with {}. Must be full path | |
EXCLUDEDIR=/Users/JohnDoe/Sites/dir\ with\ space/excludedDirectory | |
# to exclude multiple files, wrap with {} | |
EXCLUDEFILE=file.csv | |
SEARCHDIR=/Users/JohnDoe/Sites/dir\ with\ space/ | |
OLDIFS=$IFS | |
IFS=',' | |
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; } | |
while read newId oldId | |
do | |
#echo "newId: $newId" | |
#echo "oldId: $oldId" | |
grep -RIlZ --exclude-dir="$EXCLUDEDIR" --exclude "$EXCLUDEFILE" "$oldId" "$SEARCHDIR" | while read -r line ; do | |
#echo "$line" | |
sed -i "" "s/$oldId/$newId/g" """$line""" | |
done | |
done < $INPUT | |
IFS=$OLDIFS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment