Created
July 26, 2023 08:44
-
-
Save rubenrubiob/1e59a0f1a63b99aac92aa7b610b7c3d6 to your computer and use it in GitHub Desktop.
Iterate over a CSV and replace with sed with escaping values
This file contains hidden or 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 | |
| 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