shell実装データjsonフォーマット変換

7079 ワード

redisにhash形式で格納されたデータをjson形式に変換
例1:
redis     
test1
"dbname=test1|username=db2inst3|passwd=db2inst3|ip=192.168.1.61|port=60004|" bmw "dbname=bmw|username=db2inst3|passwd=db2inst3|ip=192.168.1.113|port=50000|" test2 "dbname=test2|username=db2inst3|passwd=db2inst3|ip=192.168.1.113|port=50000|"
shell:
awk 'BEGIN{printf "["}' echo "hgetall SrcLogInfo " | ../redis | while read line do strA="|" #num=echo ${line | wc -c} awk 'BEGIN{printf "{"}' ret=\"$line\" echo ${ret} result=$(echo $line | grep "${strA}") if [ -n "$result" ]; then awk 'BEGIN{printf "},"}' else awk 'BEGIN{printf ":"}' fi done awk 'BEGIN{printf "]"}'
     :json  
[{
        "test1": "dbname=test1|username=db2inst3|passwd=db2inst3|ip=192.168.1.61|port=60004|"
    },
    {
        "bmw": "dbname=bmw|username=db2inst3|passwd=db2inst3|ip=192.168.1.113|port=50000|"
    },
    {
        "test2": "dbname=test2|username=db2inst3|passwd=db2inst3|ip=192.168.1.113|port=50000|"
    }
]

 
例2:
redis      :
1) "db2inst1"
2) "{\"serverip\":\"192.168.1.113\",\"dbip\":\"192.168.1.225\",\"dbport\":\"50000\",\"nodename\":\"node225\",\"dbname\":\"db2_10\",\"dbaliname\":\"db2inst1\",\"instname\":\"db2inst1\"}"
3) "fea"
4) "{\"serverip\":\"192.168.1.113\",\"dbip\":\"192.168.1.225\",\"dbport\":\"50000\",\"nodename\":\"node225\",\"dbname\":\"db2_10\",\"dbaliname\":\"fea\",\"instname\":\"db2inst1\"}"
5) "bmw"
6) "{\"serverip\":\"192.168.1.113\",\"dbip\":\"192.168.1.61\",\"dbport\":\"60004\",\"nodename\":\"node61\",\"dbname\":\"test1\",\"dbaliname\":\"bmw\",\"instname\":\"db2inst3\"}"
shell:
awk 'BEGIN{printf "["}' echo "hgetall catainfo " | ../redis| while read line do i=$(($i+1)) if [[ $(($i%2)) -eq 0 ]];then printf $line, fi done awk 'BEGIN{printf "]"}'

//

 
転載先:https://www.cnblogs.com/kony9527/p/10534292.html