awk 中的良好 json 格式
good json format in awk
我一直在网上搜索,但找不到解决我的 awk 打印输出的方法。
这是我的awk(打印结束代码):
END {
for ( j = 1; j <= i; j++ )
{ print "{\"name\": \"%s\",\"address\": \"%s\", \"email\": \"%s\" },\n",
name, address, email
}
}' "$file"
显然,结果是这个输出:
{"name": "Kimberly":"AMAZON", "address":"NY", "email":"kim@example.com" },
{"name": "Manny","address": "Nashville", "email": "manny@example.com" },
{"name": "Nina","address": "Mountain Hills", "email": "iamnina@example.com" },
但我想生成这样的输出:
[
{"name": "Kimberly":"AMAZON", "address":"NY", "email":"kim@example.com"},
{"name":"Manny","address": "Nashville", "email": "manny@example.com" },
{"name": "Nina","address": "Mountain Hills", "email": "iamnina@example.com" }]
希望大家帮帮忙。
非常感谢您。
根据你的问题,我假设你只需要格式化。
所以这是您可以做到的一种方法
END{print "["
for(j=1;j<=i;j++)
print "{\"name\": \""name"\", \"address\":\""address"\", \"email\":\""email"\"}"(j==i?"]":",")}
打印第一个[
然后打印行,使用三元运算符检查它是否是最后一行并打印 ]
如果是,否则打印 ,
.
我一直在网上搜索,但找不到解决我的 awk 打印输出的方法。
这是我的awk(打印结束代码):
END {
for ( j = 1; j <= i; j++ )
{ print "{\"name\": \"%s\",\"address\": \"%s\", \"email\": \"%s\" },\n",
name, address, email
}
}' "$file"
显然,结果是这个输出:
{"name": "Kimberly":"AMAZON", "address":"NY", "email":"kim@example.com" },
{"name": "Manny","address": "Nashville", "email": "manny@example.com" },
{"name": "Nina","address": "Mountain Hills", "email": "iamnina@example.com" },
但我想生成这样的输出:
[
{"name": "Kimberly":"AMAZON", "address":"NY", "email":"kim@example.com"},
{"name":"Manny","address": "Nashville", "email": "manny@example.com" },
{"name": "Nina","address": "Mountain Hills", "email": "iamnina@example.com" }]
希望大家帮帮忙。 非常感谢您。
根据你的问题,我假设你只需要格式化。
所以这是您可以做到的一种方法
END{print "["
for(j=1;j<=i;j++)
print "{\"name\": \""name"\", \"address\":\""address"\", \"email\":\""email"\"}"(j==i?"]":",")}
打印第一个[
然后打印行,使用三元运算符检查它是否是最后一行并打印 ]
如果是,否则打印 ,
.