Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
1 1 1 2 2 2 3 3
假设我有一个包含这些数字的文件,我想像这样计算这些数字
(数量):(发生)
1:3 2:3 3:2
如何在 shell 脚本中执行此操作?
你可以用这个
sort a.dat | uniq -c | awk '{print $2, ":", $1}'
如果你真的被空白困扰
sort a.dat | uniq -c | awk '{print $2":"$1}'
只是awk:
awk '!a[$0]++{b[++i]=$0}END{for(i=1;i in b;++i)print b[i]":"a[b[i]]}' file
输出: