为什么这个字段分隔符正则表达式不允许我提取所需的值?

时间:2017-02-27 22:54:45

标签: 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",   "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

我做错了什么?

1 个答案:

答案 0 :(得分:3)

输入有点不规则,并不清楚数组sum的用途是什么,但要准确地告诉你你要求的是什么:

awk -F'^.*"lastName": "|",' '{ print $2 }' sumacomando

字段分隔符正则表达式'^.*"lastName": "|",'匹配从行的开头到"lastName": "的所有内容,然后匹配",,以便第二个字段 - $2 - 有效地成为lastName字段的相关值的内容。