使用curl,我从我的TP-Link 路由器获取一些统计信息。我将该信息放入一个文本文件中。但是,我需要 JSON 格式的此信息,以便我可以在其他地方使用它。文本文件的格式如下:
[0,0,0,0,0,0]0
enable=1
interval=10
action=0
[1,0,0,0,0,0]1
ipAddress=3232235848
macAddress=EC:0E:C4:4F:XX:XX
totalPkts=201
totalBytes=22914
currPkts=0
currBytes=0
currIcmp=0
currUdp=0
currSyn=0
currIcmpMax=0
currUdpMax=15
currSynMax=0
[2,0,0,0,0,0]1
ipAddress=3232235829
macAddress=00:16:E8:22:XX:XX
totalPkts=972
totalBytes=98730
currPkts=0
currBytes=0
currIcmp=0
currUdp=0
currSyn=0
currIcmpMax=0
currUdpMax=14
currSynMax=4
[3,0,0,0,0,0]1
ipAddress=3232235842
macAddress=70:14:A6:8B:XX:XX
totalPkts=154566
totalBytes=15116490
currPkts=0
currBytes=0
currIcmp=0
currUdp=0
currSyn=0
currIcmpMax=9
currUdpMax=58
currSynMax=60
我需要一个 JSON 类似的东西:
{"ipAddress" : "3232235848",
"macAddress" : "EC:0E:C4:4F:XX:XX",
"totalPkts" : "201",
"totalBytes" : "22914",
"currPkts" : "0",
"currBytes" : "0",
"currIcmp" : "0",
"currUdp" : "0",
"currSyn" : "0",
"currIcmpMax" : "0",
"currUdpMax" : "15",
"currSynMax" : "0"}
{"ipAddress" : "3232235829", ...
您能告诉我最有效的方法吗?如果可能的话,我更喜欢 bash 脚本?尝试将“| jq -R -s -c 'split("\n")'”放在curl命令的末尾,但它看起来像:{“ipAddress = 3232235848”,“macAddress = EC:0E:C4:4F :XX:XX”...
答案1
sed破解:
sed -E -e 's/\[([0-9],){5}[0-9]\][0-9]+/}{/g; s/([[:alnum:]]+)=([[:alnum:]]+)/"\1" : "\2",/;
s/,\n}/}/; 1s/.*/{/; $s/.*/&}/' yourfile | sed -Ez 's/,\n?}/ }\n/g'
输出:
{
"enable" : "1",
"interval" : "10",
"action" : "0" }
{
"ipAddress" : "3232235848",
"macAddress" : "EC",:0E:C4:4F:XX:XX
"totalPkts" : "201",
"totalBytes" : "22914",
"currPkts" : "0",
"currBytes" : "0",
"currIcmp" : "0",
"currUdp" : "0",
"currSyn" : "0",
"currIcmpMax" : "0",
"currUdpMax" : "15",
"currSynMax" : "0" }
{
"ipAddress" : "3232235829",
"macAddress" : "00",:16:E8:22:XX:XX
"totalPkts" : "972",
"totalBytes" : "98730",
"currPkts" : "0",
"currBytes" : "0",
"currIcmp" : "0",
"currUdp" : "0",
"currSyn" : "0",
"currIcmpMax" : "0",
"currUdpMax" : "14",
"currSynMax" : "4" }
{
"ipAddress" : "3232235842",
"macAddress" : "70",:14:A6:8B:XX:XX
"totalPkts" : "154566",
"totalBytes" : "15116490",
"currPkts" : "0",
"currBytes" : "0",
"currIcmp" : "0",
"currUdp" : "0",
"currSyn" : "0",
"currIcmpMax" : "9",
"currUdpMax" : "58",
"currSynMax" : "60" }
答案2
perl -lne 'BEGIN{*1 = sub {print q<{>, join(",\n", splice @A, 0, @A), q<}>}}
next if $. == 1;
/^\[(?:\d+,?)+\]\d+$/ and &1,next;
push @A, join q/ : /, map qq/"$_"/, split /=/;
eof && &1;
' yourfile
答案3
使用磨坊主( mlr
) 经过一些简单的预处理后sed
:
sed 's/^\[.*//' file | mlr --irs lflf --ifs lf --ojson cat
这首先用于sed
清除以 开头的行[
。随后创建的空行将作为 Miller 的记录分隔符。
然后,Miller 将输入转换为 JSON,而无需进一步修改数据。为了使 Miller 能够理解数据,我们指定输入记录分隔符为双换行,输入字段分隔符为单换行。
考虑到问题中的示例数据,此命令将产生以下输出:
[
{
"enable": 1,
"interval": 10,
"action": 0
},
{
"ipAddress": 3232235848,
"macAddress": "EC:0E:C4:4F:XX:XX",
"totalPkts": 201,
"totalBytes": 22914,
"currPkts": 0,
"currBytes": 0,
"currIcmp": 0,
"currUdp": 0,
"currSyn": 0,
"currIcmpMax": 0,
"currUdpMax": 15,
"currSynMax": 0
},
{
"ipAddress": 3232235829,
"macAddress": "00:16:E8:22:XX:XX",
"totalPkts": 972,
"totalBytes": 98730,
"currPkts": 0,
"currBytes": 0,
"currIcmp": 0,
"currUdp": 0,
"currSyn": 0,
"currIcmpMax": 0,
"currUdpMax": 14,
"currSynMax": 4
},
{
"ipAddress": 3232235842,
"macAddress": "70:14:A6:8B:XX:XX",
"totalPkts": 154566,
"totalBytes": 15116490,
"currPkts": 0,
"currBytes": 0,
"currIcmp": 0,
"currUdp": 0,
"currSyn": 0,
"currIcmpMax": 9,
"currUdpMax": 58,
"currSynMax": 60
}
]
为了避免结果数组中出现第一个对象,请将cat
操作更改为filter
避免输出第一个记录的操作:
sed 's/^\[.*//' file | mlr --irs lflf --ifs lf --ojson filter 'NR > 1'
您是否还愿意不是将生成的 JSON 对象包装在数组中,然后使用 Miller --no-jlistwrap
:
sed 's/^\[.*//' file | mlr --irs lflf --ifs lf --ojson --no-jlistwrap filter 'NR > 1'
--ojson
...或者通过更改为以下内容来更改为每行一个对象的 JSON 输出格式--ojsonl
:
sed 's/^\[.*//' file | mlr --irs lflf --ifs lf --ojsonl filter 'NR > 1'
...这会产生
{"ipAddress": 3232235848, "macAddress": "EC:0E:C4:4F:XX:XX", "totalPkts": 201, "totalBytes": 22914, "currPkts": 0, "currBytes": 0, "currIcmp": 0, "currUdp": 0, "currSyn": 0, "currIcmpMax": 0, "currUdpMax": 15, "currSynMax": 0}
{"ipAddress": 3232235829, "macAddress": "00:16:E8:22:XX:XX", "totalPkts": 972, "totalBytes": 98730, "currPkts": 0, "currBytes": 0, "currIcmp": 0, "currUdp": 0, "currSyn": 0, "currIcmpMax": 0, "currUdpMax": 14, "currSynMax": 4}
{"ipAddress": 3232235842, "macAddress": "70:14:A6:8B:XX:XX", "totalPkts": 154566, "totalBytes": 15116490, "currPkts": 0, "currBytes": 0, "currIcmp": 0, "currUdp": 0, "currSyn": 0, "currIcmpMax": 9, "currUdpMax": 58, "currSynMax": 60}