awk将普通文本转换成json文件

script1:

#!/bin/bash
#Date:2016-11-16
#Author:eivll0m

awk -F"\t" -vq='"' '{
        a[$1]=a[$1]?a[$1]"\t"$2"\t"$3"\t"$4:$2"\t"$3"\t"$4
        }END{
                for(i in a){
                        printf i"\t[";l=split(a[i],b,"\t");
              for(j=1;j<=l;j+=3){ printf "{h_name"":"q b[j] q",";
                   printf "level"":"q b[j+1] q",";
                   printf "addres"":"q b[j+2] q"}," } print "]" } }' yy.txt|sed 's/,]$/]/' >>yy_json.txt

script2:

#!/bin/bash
#Date:2016-11-17
#Author:eivll0m

awk -F"\t" -vq='"' '
        function json(x,y){printf("%s:%s,",q x q, q y q)}
        function json2(x,y){printf("%s:%s",q x q, q y q)}
        function json3(x,y){printf("%s:%s",q x q, y)}
                {
                        printf "{";
              json("id",$2); printf q"essential_info"q":{"; json("name",$1); json("introduction",$3); json("medical_insurance",$4); json("alias",$5);
              json("position",$6); json("infectivity",$7); json("route_of_transmission",$8); json2("multiple_population",$9);
              printf "},"; printf q"symptom"q":{"; json("typical",$10); json2("content",$11);
              printf "},"; json("pathogeny",$12);
              json("prevent",$13); printf q"inspect"q":{"; json("item",$14); json2("content",$15);
              printf "},"; json("differentiate",$16); printf q"treat"q":{"; json("mode",$17); json2("content",$18);
              printf "},"; json("nurse",$19); json("complication",$20); json3("hospital",$21); print "}" }' jibin.TXT >jibin.json

 

posted on 2016-11-17 10:25  K‘e0llm  阅读(3141)  评论(0编辑  收藏  举报

导航