我的输入文件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
我做错了什么?
答案 0 :(得分:3)
输入有点不规则,并不清楚数组sum
的用途是什么,但要准确地告诉你你要求的是什么:
awk -F'^.*"lastName": "|",' '{ print $2 }' sumacomando
字段分隔符正则表达式'^.*"lastName": "|",'
匹配从行的开头到"lastName": "
的所有内容,然后匹配",
,以便第二个字段 - $2
- 有效地成为lastName
字段的相关值的内容。