我有以下数据:
{
foo,
bar,
qux,
zuu,
sdf,
sdfasdf
}
{
abc,
efg,
hij,
klm,
nop,
qrs
}
我想要做的是将它们转换为一行和逗号分隔的块:
foo,bar,qux,zuu,sdf,sdfasdf
abc,efg,hij,klm,nop,qrs
最好的unix shell编程方法是什么?
我正在尝试处理JSON数据,如下所示:
{
"Items": [
{
"A": {
"S": "http://"
},
"B": {
"N": "1"
},
"C": {
"N": "0"
},
"D": {
"N": "0"
},
"E": {
"S": "2110221641826713463977"
},
"F": {
"N": "2.5"
},
"G": {
"S": " "
},
"H": {
"B": "IA"
},
"I": {
"N": "0"
},
"J": {
"N": "0"
}
},
{
"A": {
"S": "http://"
},
"B": {
"N": "0"
},
"C": {
"N": "1"
},
"D": {
"N": "2"
},
"E": {
"S": "67"
},
"F": {
"N": "0"
},
"G": {
"S": " "
},
"H": {
"B": "Agh"
},
"I": {
"N": "9"
},
"J": {
"N": "1"
}
},
]
}
我正在使用JQ解析如下:
jq -r '.Items[]' | jq -r '{(.A.S): [.A.S, .B.B, .C.N, .D.S, .E.N, .F.N, .G.N, .H.N, .I.S]}'
这时解析JSON但保留上面的数据。
答案 0 :(得分:4)
使用gnu awk
可以在一个命令中完成:
gawk -v RS='\n?[{}]\n?' 'NF{gsub(/ *\n */, ""); print}' file
foo,bar,qux,zuu,sdf,sdfasdf
abc,efg,hij,klm,nop,qrs
答案 1 :(得分:2)
这是你在找什么?
$ jq -c '.Items[]|[.[][]]' test.json
["http://","1","0","0","2110221641826713463977","2.5"," ","IA","0","0"]
["http://","0","1","2","67","0"," ","Agh","9","1"]
答案 2 :(得分:1)
使用tr
和sed
:
tr -d '\n' < file | sed -r 's@\{ @@g; s@, @,@g; s@}@\n@g'
输出:
foo,bar,qux,zuu,sdf,sdfasdf
abc,efg,hij,klm,nop,qrs
此处还有一个仅使用sed
的表单:
sed -n '$!{ H; b; }; g; s|\n||g; s|{ ||g; s|, |,|g; s|}|\n|g; p' file
答案 3 :(得分:1)
这是awk
awk '!/^[{}]/ {printf "%s"(/,$/?"":RS),$1}' file
foo,bar,qux,zuu,sdf,sdfasdf
abc,efg,hij,klm,nop,qrs
答案 4 :(得分:0)
您也可以使用此sed
方法
sed ':loop ;N; /\n\}$/{P;d;t} ;/^{/{D} ;s/ *\(.*\)\n /\1/g ;t loop' filename
答案 5 :(得分:0)
$ awk -v RS=} '{$1=$1; gsub(/[{ ]/,"")} $0' file
foo,bar,qux,zuu,sdf,sdfasdf
abc,efg,hij,klm,nop,qrs