Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save rubenrubiob/1e59a0f1a63b99aac92aa7b610b7c3d6 to your computer and use it in GitHub Desktop.

Select an option

Save rubenrubiob/1e59a0f1a63b99aac92aa7b610b7c3d6 to your computer and use it in GitHub Desktop.
Iterate over a CSV and replace with sed with escaping values
#!/bin/bash
file_with_replaces="some-csv-file"
folder_to_look_into="folder/to/look/into"
filename_extension="*.txt"
while IFS=" | " read -r search_for replace_to
do
# From: https://stackoverflow.com/a/2705678
escaped_search_for=$(printf '%s\n' "$search_for" | sed -e 's/[\/&]/\\&/g')
escaped_replace_to=$(printf '%s\n' "$replace_to" | sed -e 's/[\/&]/\\&/g')
find "$folder_to_look_into" -type f -name "$filename_extension" -exec sed -i -e "s#$escaped_search_for#$escaped_replace_to#g" "{}" \;
done < $file_with_replaces
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment