Skip to content

Instantly share code, notes, and snippets.

@cmdruid
Last active May 2, 2022 04:59
Show Gist options
  • Save cmdruid/47cd68f98f72b176b1eb6b030325a20d to your computer and use it in GitHub Desktop.
Save cmdruid/47cd68f98f72b176b1eb6b030325a20d to your computer and use it in GitHub Desktop.
Simple script that extracts the value for a given key within a JSON output.
#!/bin/sh
## Takes an input pipe of JSON, and outputs the value for a given key.
set -e
usage() {
printf "
Takes an input pipe of JSON, and outputs the value for a given key. \n
Usage: some_JSON_output | jgrep key \n
"
}
## If argument empty or help, display help message.
if [ -z "$1" ] || [ "$1" = "--help" ]; then usage && exit 0; fi
## Grep the key from the JSON input, then sanitize.
grep $1 | awk '{print $2}' | tr -d ',"' < /dev/stdin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment