我们有一个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等简单文本处理工具的专家,但我确实尝试了一段时间,但没有成功。
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}
最好的方法是什么?
因为它不是JSON,您只想拆分:
input_file
{this is the first block},{this is the second block, really},{this is the third you're kidding:no}
sed 's/},/}n/g' input_file
output
{this is the first block}
{this is the second block, really}
{this is the third you're kidding:no}
Awk替代方案:
awk 'BEGIN { RS="(,?{)|(},?)" } /[[:alnum:]]/ { print $0="{"$0"}" }' file
将记录分隔符设置为一个或多个逗号和{或}以及一个或更多个逗号。然后,当遇到字母数字字符串时,在字符串前面加前缀{,追加}并打印