我有一个制表符分隔的文件,如下所示:
A 1234
A 123245
A 4546
A 1234
B 24234
B 4545
C 1234
C 1234
Output:
A 3
B 2
C 1
Run Code Online (Sandbox Code Playgroud)
基本上,我需要属于第一列的唯一值的计数,所有这些都在带有管道的一个突击队中。正如您所看到的,可能会有一些重复项,例如“A 1234”。我对 awk 或 cut 有一些想法,但似乎都不起作用。他们只是打印出所有唯一对,而我需要考虑第一列中的值来计算第二列中的唯一值。
awk -F " "'{print $1}' file.tsv | uniq -c
cut -d' ' -f1,2 file.tsv | sort | uniq -ci
Run Code Online (Sandbox Code Playgroud)
我非常感谢你的帮助!先感谢您。
有了完整的awk
解决方案,您可以尝试以下操作。
awk 'BEGIN{FS=OFS="\t"} !found[$0]++{val[$1]++} END{for(i in val){print i,val[i]}}' Input_file
Run Code Online (Sandbox Code Playgroud)
说明:对上述内容添加详细说明。
awk ' ##Starting awk program from here.
BEGIN{
FS=OFS="\t"
}
!found[$0]++{ ##Checking condition if 1st and 2nd column is NOT present in found array then do following.
val[$1]++ ##Creating val with 1st column inex and keep increasing its value here.
}
END{ ##Starting END block of this progra from here.
for(i in val){ ##Traversing through array val here.
print i,val[i] ##Printing i and value of val with index i here.
}
}
' Input_file ##Mentioning Input_file name here.
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1485 次 |
最近记录: |