我一直在网上搜索,但无法找到我的awk打印输出的解决方案。
这是我的awk(打印的END代码):
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" }]
我希望任何人都可以提供帮助。 非常感谢你。
答案 0 :(得分:1)
根据您的问题,我假设您只想要格式化。
所以这是你可以做到的一种方式
END{print "["
for(j=1;j<=i;j++)
print "{\"name\": \""name"\", \"address\":\""address"\", \"email\":\""email"\"}"(j==i?"]":",")}
打印第一个[
然后打印线条,使用三元运算符检查它是否是最后一行并打印]
,否则打印,
。