Skip to content

Instantly share code, notes, and snippets.

@flying-sausages
Last active December 7, 2020 16:19
Show Gist options
  • Save flying-sausages/9491da4db64940e59c99231912e5169c to your computer and use it in GitHub Desktop.
Save flying-sausages/9491da4db64940e59c99231912e5169c to your computer and use it in GitHub Desktop.
Search and replace using key-value pairs
#!/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