我在我的一个系统上运行一个命令,它会像这样吐出JSON“漂亮”格式:
[
{
"server": "servename1",
"i.p": 127.0.0.1,
"domain": "generic",
"OS": "RHEL",
"Version": 7.0
},
{
"server": "servename2",
"i.p": 127.0.0.1,
"domain": "generic",
"OS": "RHEL",
"Version": 7.0
},
{
"server": "servename3",
"i.p": 127.0.0.1,
"domain": "generic",
"OS": "RHEL",
"Version": 7.0
}
]
我需要像这样在一个衬里中解析这些段落:
[{"server":"servename1","i.p":127.0.0.1,"domain":"generic","OS":"RHEL","Version":7.0},
{"server":"servename2","i.p":127.0.0.1,"domain":"generic","OS":"RHEL","Version":7.0},
{"server":"servename3","i.p":127.0.0.1,"domain":"generic","OS":"RHEL","Version":7.0},]
最简单的方法是什么?我尝试使用SED和JQ,但无法得到它。
答案 0 :(得分:2)
如果您使用换行符分隔的JSON:
,则可以在jq中尝试此操作$ jq -c ".[]" test.json
{"server":"servename1","i.p":"127.0.0.1","domain":"generic","OS":"RHEL","Version":7}
{"server":"servename2","i.p":"127.0.0.1","domain":"generic","OS":"RHEL","Version":7}
{"server":"servename3","i.p":"127.0.0.1","domain":"generic","OS":"RHEL","Version":7}
请注意,我必须引用您示例中的IP地址,因为您发布的JSON不是有效的JSON。
答案 1 :(得分:0)
如果您的输入始终是常规的:
[
如果这不适合您的实际输入,请编辑您的问题以包含更真实的代表性样本输入。
要跳过]
和$ awk '!/^[][]/{ORS=(/},/?RS:""); gsub(/[[:blank:]]+/,""); sub(/}$/,"},\n"); print}' file
{"server":"servename1","i.p":127.0.0.1,"domain":"generic","OS":"RHEL","Version":7.0},
{"server":"servename2","i.p":127.0.0.1,"domain":"generic","OS":"RHEL","Version":7.0},
{"server":"servename3","i.p":127.0.0.1,"domain":"generic","OS":"RHEL","Version":7.0},
行:
{{1}}