Skip to content

Instantly share code, notes, and snippets.

@gentunian
Created November 19, 2018 17:53
Show Gist options
  • Save gentunian/19574ee5c6973f0145b2a753882cc947 to your computer and use it in GitHub Desktop.
Save gentunian/19574ee5c6973f0145b2a753882cc947 to your computer and use it in GitHub Desktop.
redis hash to json with awk
redis-cli --raw -d "," hgetall mykey | awk -F',' 'BEGIN { print "{" }
{ for (i=1; i < NF; i++) {
if (i%2) {
printf "\"%s\"", $i
} else {
printf ":%s,\n", $i
}
}
}
END { printf ":%s\n}", $NF}'
@gentunian
Copy link
Author

if your values are string, you need to quote them. This is done in the END block of awk and in the else block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment