Skip to content

Instantly share code, notes, and snippets.

@mvogelgesang
Last active April 29, 2022 18:56
Show Gist options
  • Save mvogelgesang/3c469cac7fa4ec8c193cf30a770129a9 to your computer and use it in GitHub Desktop.
Save mvogelgesang/3c469cac7fa4ec8c193cf30a770129a9 to your computer and use it in GitHub Desktop.
Given an input file containing a newID value and oldId value, perform a mass find/replace across a series of files
#!/bin/bash
# set -x
# CSV FILE MUST HAVE EMPTY LINE AT END AND NO ""
# Given an input file containing a newID value and oldId value, perform a mass find/replace across a series of files
INPUT=inputdata.csv
EXCLUDEDIR=/Users/mv/folder\ to\ exclude
EXCLUDEFILE=fileToExclude.csv
SEARCHDIR=/Users/mv/search\ directory
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