使用 awk关联数组查找第一列中每个唯一值的第二列中唯一值的计数?
a,x
a,y
a,z
a,w
b,x
b,y
a,x
b,x
o / p应该是
a,4
b,2
答案 0 :(得分:1)
类似awk
$ awk -F, -v OFS=, '{a[$1]+=!b[$1,$2]++} END{for(k in a) print k,a[k]}' file
a,4
b,2
另一种方法
$ sort -u file | cut -d, -f1 | uniq -c | awk '{print $2","$1}'
答案 1 :(得分:0)
<强>输入强>
$ cat file
a,x
a,y
a,z
a,w
b,x
b,y
a,x
b,x
<强>输出强>
$ awk 'BEGIN{FS=OFS=","}!(($1,$2) in b){b[$1,$2]; a[$1]++}END{for(i in a)print i,a[i]}' file
a,4
b,2
可读版本
awk 'BEGIN{
FS=OFS="," # Set input and output separator
}
!(($1,$2) in b){ # check index col1,col2 exists in array b
b[$1,$2] # if not exists then its unique combination
a[$1]++ # set array b with index col1,col2 and increment count of array a
}
END{ # finally loop through array a and print contents
for(i in a)
print i,a[i]
}' file