Awk 为什么';这个字段分隔符regex是否允许我提取所需的值?
我的输入文件Awk 为什么';这个字段分隔符regex是否允许我提取所需的值?,awk,Awk,我的输入文件sumacomando如下所示: "firstName": "gdrgo", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "lastName": "222",dfg "xxxxx": "John", "firstName": "beto", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",
sumacomando
如下所示:
"firstName": "gdrgo", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "lastName": "222",dfg
"xxxxx": "John", "firstName": "beto", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "lastName": "111","xxxxx": "John",
"xxxxx": "John", "firstName": "beto", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "lastName": "111","xxxxx": "John",
"xxxxx": "John", "xxxxx": "John", "firstName": "beto2", "xxxxx": "John","lastName": "555", "xxxxx": "John","xxxxx": "John",
"xxxxx": "John", "xxxxx": "John", "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",
"firstName": "gdrgo", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "lastName": "222",dfg
"xxxxx": "John", "xxxxx": "John", "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",
我使用以下命令:
awk -v RS="\n" \
-v FS='firstName": "|",[^+]*lastName": "|",' \
'{sum[$1]=$2;} {print sum[$1]}' sumacomando
哪些产出:
gdrgo
111
111
555
444
gdrgo
444
但我预料到:
222
111
111
555
444
222
444
我做错了什么?输入有点不规则,不清楚数组
求和的目的是什么,但要准确地给出您想要的:
awk -F'^.*"lastName": "|",' '{ print $2 }' sumacomando
字段分隔符regex“^.*“lastName”:“|”和“
匹配从行的开头到“lastName”:“
,然后是”,
,因此,第二个字段-$2
-有效地成为lastName
字段关联值的内容。数据文件是否真的像您显示的那样在逗号后加空格?你的字段分隔符正则表达式很难阅读-你不认为你应该更加尊重Awk吗?你认为中期选举能处理什么?您有,[^+]*lastName:“
,在我看来,它像一个双引号、一个逗号、零个或多个不是加号的字符,然后是lastName
、一个双引号、冒号、空格、双引号。您是否简单地打印出字段(明确地),以便您可以看到Awk如何解释您的正则表达式?(for(i=1;i