如何使用awk从txt分组数据?

时间:2015-09-25 15:57:05

标签: awk grouping

我有一组数据,如:

firstname lastname state zipcode
 john         doe   oh   44110
jane          doe   ca   90210
john          doe2  ca   90210
jane          doe   pa   19810

我的问题是使用AWK如何显示数据:

firstname lastname state zipcode
jane          doe   ca   90210
john          doe2  ca   90210

我想列出具有特定邮政编码的特定州的所有人

我试过了

awk '{print $1, $2, $7(state)= "California", $8(zip)= "90210"}'

显示:

firstname lastname California 90210
 john     doe      california 90210
jane      doe      California 90210
James     doe      California 90210

而不是:

firstname lastname state zip
john     doe      California 90210
jane      doe      California 90210
James     doe      California 90210

4 个答案:

答案 0 :(得分:2)

awk 'NR==1 || /ca/' file
firstname lastname state zipcode
jane          doe   ca   90210
john          doe2  ca   90210

答案 1 :(得分:1)

试试这个:

awk 'NR==1||($3=="ca"&&$4==90210)' file

这将打印第一行,以及第三列为“ca”且第四列为90210的所有其他行。

答案 2 :(得分:1)

邮政编码已包含状态信息,指定它将是多余的。

$ awk -v zip="90210" 'NR==1 || $NF==zip' file

会给你

firstname lastname state zipcode
jane          doe   ca   90210
john          doe2  ca   90210

我认为您想要将状态代码中的显示更改为状态名称

$ awk -v zip="90210" 'NR==1 || $NF==zip {sub(/ca/,"California",$3);print} ' file
firstname lastname state zipcode
jane doe California 90210
john doe2 California 90210

然而,现在间距已经破裂。您必须将输出文件分隔符更改为选项卡。

$ awk -v zip="90210" -v OFS="\t" 'NR==1 || $NF==zip {sub(/ca/,"California",$3);print} ' states
firstname lastname state zipcode
jane    doe     California      90210
john    doe2    California      90210

几乎在那里,除了第一行。您必须强制awk替换字段分隔符

$ awk -v zip="90210" -v OFS="\t" 'NR==1{$1=$1;print;next} $NF==zip {sub(/ca/,"California",$3);print} ' states
firstname       lastname        state   zipcode
jane    doe     California      90210
john    doe2    California      90210

答案 3 :(得分:1)

如果您指定zip并且解决了尝试将“California”映射到“ca”的问题,则无需指定状态。只是做:

$ awk -v zip="90210" '(NR==1) || ($4==zip)' file
firstname lastname state zipcode
jane          doe   ca   90210
john          doe2  ca   90210