Last active
December 7, 2020 16:19
-
-
Save flying-sausages/9491da4db64940e59c99231912e5169c to your computer and use it in GitHub Desktop.
Search and replace using key-value pairs
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 | |
# We're gonna assume your "key-value" pairs is in a normal bash array. | |
fakedict=( | |
key1 value1 | |
key2 value2 | |
key3 value3 | |
) | |
# Constructs a string like "s/key1/value1/g; s/key2/value2/g; s/key3/value3/g" | |
sedPattern="$(printf 's/%s/%s/g; ' "${fakedict[@]}")" | |
# Use the pattern string however you need | |
sed -i "$sedString" /path/to/file.txt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment