Created
May 24, 2020 09:04
-
-
Save donalod/8c1bf9a326d79402cbcf25c2b602af16 to your computer and use it in GitHub Desktop.
JQ Replace with Script
This file contains 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
#!/usr/bin/env bash | |
if [[ ${#1} = 0 ]]; then | |
echo "Note: First edit the script to update the JSON key and value to change." | |
echo "Usage: ./jq_replace_key_value_in_input_files.sh <file_glob_pattern>" | |
exit 0; | |
fi | |
key="og_image_url" | |
new_value="https://blah.domain.com/img/new_og_image.png" | |
# Below the key is "og_image_url" | |
for i in "$@"; do | |
echo -e "Looking : in $i for \"${key}\" to change to \"$new_value\"" | |
jq -e .$key $i | |
if [[ $? != 0 ]]; then | |
echo -e "Cannot find : key in $i" | |
continue | |
fi | |
cp $i $i.bak | |
echo -e "Replacing : Key \"$key\" in \"$i\"" | |
echo -e "Note : Old value of \"$key\" in \"$i\" was $(jq -e .$key $i)" | |
jq -r --arg KEY "$key" --arg NEW_VALUE "$new_value" '.[$KEY] = $NEW_VALUE' $i > $i.tmp && cp $i.tmp $i && rm $i.tmp | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment