我有一个csv文件
value name date sentence
0000 name1 date1 I want apples
0021 name2 date1 I want bananas
0212 name3 date2 I want cars
0321 name1 date3 I want pinochio doll
0123 name1 date1 I want lemon
0100 name2 date1 I want drums
1021 name2 date1 I want grape
2212 name3 date2 I want laptop
3321 name1 date3 I want Pot
4123 name1 date1 I want WC
2200 name4 date1 I want ramen
1421 name5 date1 I want noodle
2552 name4 date2 I want film
0211 name6 date3 I want games
0343 name7 date1 I want dvd
我想在name选项卡中找到唯一的值(我知道我必须使用-f 2,但是我还想知道它们出现了多少次/它们造出的句子数量)。
eg: name1,5
name2,3
name3,2
name4,2
name5,1
name6,1
name7,1
然后我想再做一个关于每个人的数据
1 appearance, 3
2 appearance ,2
3 appearance ,1
4 appearance ,0
5 appearance ,1
最佳答案
第一部分的答案是使用下面的awk
awk -F" " 'NR>1 { print $2 } ' jerome.txt | sort | uniq -c
在第二部分中,可以通过Perl进行管道传输,得到如下结果
> awk -F" " 'NR>1 { print $2 } ' jerome.txt | sort | uniq -c | perl -lane '{$app{$F[0]}++} END {@c=sort keys %app; foreach($c[0] ..$c[$#c]) {print "$_ appearance,",defined($app{$_})?$app{$_}:0 }}'
1 appearance,3
2 appearance,2
3 appearance,1
4 appearance,0
5 appearance,1
>
编辑1:
第二部分使用Perl一行程序
> perl -lane '{$app{$F[1]}++ if $.>1} END {$app2{$_}++ for(values %app);@c=sort keys %app2;foreach($c[0] ..$c[$#c]) {print "$_ appearance,",$app2{$_}+0}}' jerome.txt
1 appearance,3
2 appearance,2
3 appearance,1
4 appearance,0
5 appearance,1
>
关于linux - 搜索唯一值的数量及其出现的次数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52945695/