Created
November 19, 2018 17:53
-
-
Save gentunian/19574ee5c6973f0145b2a753882cc947 to your computer and use it in GitHub Desktop.
redis hash to json with awk
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
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}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if your values are string, you need to quote them. This is done in the END block of awk and in the else block.