[英]Convert string with delimiter to an array in Json using AWK
I have below style format json and I would like to convert the values in double quotes with delimiter comma to array of elements. 我有下面的样式格式json,我想用定界符逗号将双引号中的值转换为元素数组。
The json field I would like to convert is a part of huge Json file. 我想转换的json字段是巨大的Json文件的一部分。
So I need to find the field 所以我需要找到领域
Convert 兑换
Before
{"name": "john, jane, gordon, matthew"}
to
After
{"field": ["john","jane","gordon", "matthew"]}
If your Json file has entries in a single line, then this AWK program works for your example: 如果您的Json文件在一行中包含条目,则此AWK程序适用于您的示例:
{
sub(": *",": [")
sub("}","]}")
gsub(", *","\",\"")
print
}
If your data is in a single line, replace sub
with gsub
. 如果数据在一行中,请将sub
替换为gsub
。
如果所有行的格式都相似,请尝试一下
awk '{print gensub(/\"/,"[","3")}' | awk '{print gensub(/\"}$/,"]}","")}' filename
I think sed is also good like in this case 我认为sed在这种情况下也很好
$ sed -r 's/"([^"]+)".*"([^"]+)"/{"\1": ["\2"]}/; s/ ?, ?/","/g' file
{{"name": ["john","jane","gordon","matthew"]}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.