如何用awk重新格式化JSON?

时间:2014-04-07 07:30:03

标签: bash sed awk

我在下面的文本文件中提供了相关信息。

{"00:00:00:00:00:00:02:03":[{"controllerId":"onos-primary"}],"00:00:00:00:00:00:02:02":[{"controllerId":"onos-primary"}],"00:00:00:00:00:00:02:01":[{"controllerId":"onos-primary"}],"00:00:00:00:00:00:02:06":[{"controllerId":"onos-primary"}],"00:00:00:00:00:00:02:05":[{"controllerId":"onos-primary"}],"00:00:00:00:00:00:02:04":[{"controllerId":"onos-primary"}]}

我希望这些信息是这样的。

{
"00:00:00:00:00:00:02:03":[{"controllerId":"onos-primary"}],
"00:00:00:00:00:00:02:02":[{"controllerId":"onos-primary"}],
"00:00:00:00:00:00:02:01":[{"controllerId":"onos-primary"}],
"00:00:00:00:00:00:02:06":[{"controllerId":"onos-primary"}],
"00:00:00:00:00:00:02:05":[{"controllerId":"onos-primary"}],
"00:00:00:00:00:00:02:04":[{"controllerId":"onos-primary"}]
}

如何使用Linux命令行来解决这个问题。我愿意使用awk或sed或简单的bash

3 个答案:

答案 0 :(得分:2)

您可以使用

awk '{sub('/^{/', "{\n", $0); sub('/}$/', "\n}", $0); gsub('/,/', ",\n", $0); print;}' filename

答案 1 :(得分:1)

我的建议是python?

python -m json.tool < file.json 

使用语法高亮显示:

python -m json.tool < file.json | highlight --syntax js -O xterm256

答案 2 :(得分:0)

这可能适合你(GNU sed):

sed -r 's/\{(.*)\}/{\n\1\n}/;s/,/&\n/g' file