fast-replace/bash-fast-replace/bin/replace.sh

28 lines
535 B
Bash
Raw Permalink Normal View History

2023-12-30 21:35:09 +00:00
#! /bin/bash
# Do search and replace for large files
START=$(date +%s)
BASE=$(dirname $(dirname $(realpath -s $0)));
CORPUS_FILE="$BASE/csv/corpus.csv"
PAIRS_FILE="$BASE/csv/pairs.csv"
REPLACED_FILE="$BASE/csv/replaced.csv"
if [ -f "$REPLACED_FILE" ]; then
rm "$REPLACED_FILE"
fi
echo "Generating replaced.csv..."
cp "$CORPUS_FILE" "$REPLACED_FILE"
while IFS=, read -r KEY VAL
do
sed -i -e "s/$KEY/$VAL/g" "$REPLACED_FILE"
done < "$PAIRS_FILE"
END=$(date +%s)
SECONDS=`expr $END - $START`
echo "Replacement took ${SECONDS}s"