Skip to content

Instantly share code, notes, and snippets.

@mvogelgesang
Created April 13, 2020 12:15
Show Gist options
  • Save mvogelgesang/1e5b30f1619807b77c2ca673f4eba0e2 to your computer and use it in GitHub Desktop.
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
newterm1 oldterm1
newterm2 oldterm2
newterm3 oldterm3
#!/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