您好我有一个由行组成的json文件,其中每行包含类似的内容:
[{“user1”:{“key1”:”value”,”key2”:”value2”,”key3”:[{“key4”:”value4”,”key5”:”value5”}],”key6”:”value6”},”key7”:”value7},{“user2”:{“key1”:”value”,”key2”:”value2”,”key3”:[{“key4”:”value4”,”key5”:”value5”}],”key6”:”value6”},”key7”:”value7},{“user3”:{“key1”:”value”,”key2”:”value2”,”key3”:[{“key4”:”value4”,”key5”:”value5”}],”key6”:”value6”},”key7”:”value7}]
(全部在同一行)
我想创建一个文件,其中每一行都是这样的(每行一个用户):
{“user1”:{“key1”:”value”,”key2”:”value2”,”key3”:[{“key4”:”value4”,”key5”:”value5”}],”key6”:”value6”},”key7”:”value7}
{“user2”:{“key1”:”value”,”key2”:”value2”,”key3”:[{“key4”:”value4”,”key5”:”value5”}],”key6”:”value6”},”key7”:”value7}
{“user3”:{“key1”:”value”,”key2”:”value2”,”key3”:[{“key4”:”value4”,”key5”:”value5”}],”key6”:”value6”},”key7”:”value7}
我该怎么做?
答案 0 :(得分:1)
这应该有效,用换行符和,{
替换,{
,并从第二个到倒数第二个字符获取子字符串。
awk '{ gsub(",{","\n{"); {print substr($0, 2, length($0) - 2)} }' file
答案 1 :(得分:0)
编辑新版问题输入:
sed -e 's/^\[//' -e 's/]$/}/' -e 's/,{/}\n{/g'
它不那么漂亮(虽然你真的只需要最后一点 - 前两个s/
只是为了整理开始/结束[,]
$ echo '[{“user1”:{“key1”:”value”,”key2”:”value2”,”key3”:[{“key4”:”value4”,”key5”:”value5”}],”key6”:”value6”},”key7”:”value7},{“user2”:{“key1”:”value”,”key2”:”value2”,”key3”:[{“key4”:”value4”,”key5”:”value5”}],”key6”:”value6”},”key7”:”value7},{“user3”:{“key1”:”value”,”key2”:”value2”,”key3”:[{“key4”:”value4”,”key5”:”value5”}],”key6”:”value6”},”key7”:”value7}]' | sed -e 's/^\[//' -e 's/]$/}/' -e 's/,{/}\n{/g'
{“user1”:{“key1”:”value”,”key2”:”value2”,”key3”:[{“key4”:”value4”,”key5”:”value5”}],”key6”:”value6”},”key7”:”value7}}
{“user2”:{“key1”:”value”,”key2”:”value2”,”key3”:[{“key4”:”value4”,”key5”:”value5”}],”key6”:”value6”},”key7”:”value7}}
{“user3”:{“key1”:”value”,”key2”:”value2”,”key3”:[{“key4”:”value4”,”key5”:”value5”}],”key6”:”value6”},”key7”:”value7}}
答案 2 :(得分:0)
在我看来,输入是JSON(或兼容),尽管您没有说明刚刚命名为value
的部分的具体值。我为这些42
中的每一个假设了value
,然后使用适当的JSON解析来实现您想要的。我选择的解析器是Python JSON库,但其他解析器当然也可以工作。
我冒昧地修改输入。有几个关闭的大括号丢失了。
结果并不是你要求的格式,但是因为缺少右括号(我对你的问题发表了评论),你要求的语法是无意义的,我假设输出格式不是那么固定。无论如何,复杂的部分是正确的解析;输出然后很简单。
echo '[{"key1":{"key2":"value","key2":[{"key3":42,"key4":"value"}],"key5":42}},{"key1":{"key2":"value","key2":[{"key3":42,"key4":"value"}],"key5":42}},{"key1":{"key2":"value","key2":[{"key3":42,"key4":"value"}],"key5":42}},{"key1":{"key2":"value","key2":[{"key3":42,"key4":"value"}],"key5":42}}]' \
| python -c 'import json, sys; v=json.load(sys.stdin); print "\n".join(str(x) for x in v)'
答案 3 :(得分:0)
对于给定的输入,这将执行:
sed -re 's/(\{"key1)/\n\1/g' | sed -re '/^\[/ d'
但是,正如Alfe所说,你有更多的左括号而不是右括号。另外一个json解析器将是一个更好的答案。
答案 4 :(得分:0)
您可以将输入传递给perl -ple 's|({“user)|\n$1|g' |sed -re 's/^\[|\]$//g' | sed -re '/^$/ d'
:
echo '[{“user1”:{“key1”:”value”,”key2”:”value2”,”key3”:[{“key4”:”value4”,”key5”:”value5”}],”key6”:”value6”},”key7”:”value7},{“user2”:{“key1”:”value”,”key2”:”value2”,”key3”:[{“key4”:”value4”,”key5”:”value5”}],”key6”:”value6”},”key7”:”value7},{“user3”:{“key1”:”value”,”key2”:”value2”,”key3”:[{“key4”:”value4”,”key5”:”value5”}],”key6”:”value6”},”key7”:”value7}]' | perl -ple 's|({“user)|\n$1|g' |sed -re 's/^\[|\]$//g' | sed -re '/^$/ d'
{“user1”:{“key1”:”value”,”key2”:”value2”,”key3”:[{“key4”:”value4”,”key5”:”value5”}],”key6”:”value6”},”key7”:”value7},
{“user2”:{“key1”:”value”,”key2”:”value2”,”key3”:[{“key4”:”value4”,”key5”:”value5”}],”key6”:”value6”},”key7”:”value7},
{“user3”:{“key1”:”value”,”key2”:”value2”,”key3”:[{“key4”:”value4”,”key5”:”value5”}],”key6”:”value6”},”key7”:”value7}
PS。一个json解析器仍然是一个更好的答案:)