【发布时间】:2021-03-01 07:54:02
【问题描述】:
我们有一个 JSON 文件需要多行而不是单行,如下所示,
{this is the first block},{this is the second block, really},{this is the third you're kidding:no}
我们希望它是这样的,以便可以将其馈送到外部程序以毫无问题地读取它,
{this is the first block}
{this is the second block, really}
{this is the third you're kidding:no}
我不是 awk、sed、cut 等简单文本处理工具方面的专家,但我确实尝试使用 sed 有一段时间没有成功。
cat test.json | sed 's/},/\n/g'
{this is the first block
{this is the second block, really
{this is the third you're kidding:no}
最好的方法是什么?
【问题讨论】:
-
似乎您在 sed 尝试中需要
}\n而不是\n -
您的输入不是 JSON
-
要成为 JSON,它需要在一个数组中,以
[开头并以]结尾。你的真实数据是这样的吗? -
(如果是这样,
jq -c '.[]' <infile.json将完成这项工作)。 -
为了说明@CharlesDuffy的评论
echo '[{"a":0},{"b":1},{"c":2}]' | jq -c '.[]'
标签: linux bash sed scripting cut