Linux命令或脚本计算文本文件中的重复行?

tim*_*eon 110 linux text duplicates

如果我有一个带有以下内容的文本文件

red apple
green apple
green apple
orange
orange
orange
Run Code Online (Sandbox Code Playgroud)

是否有可用于获得以下结果的Linux命令或脚本?

1 red apple
2 green apple
3 orange
Run Code Online (Sandbox Code Playgroud)

bor*_*ble 208

发送它sort(将相邻的项目放在一起)然后uniq -c给出计数,即:

sort filename | uniq -c
Run Code Online (Sandbox Code Playgroud)

并且可以按排序顺序(按频率)获取该列表

sort filename | uniq -c | sort -nr
Run Code Online (Sandbox Code Playgroud)


小智 48

几乎borribles',但如果添加相同的d参数去uniq那只能说明重复.

sort filename | uniq -cd | sort -nr
Run Code Online (Sandbox Code Playgroud)

  • 为小小的“-d”音符竖起大拇指。 (2认同)

mhy*_*itz 6

uniq -c file

如果文件尚未排序:

sort file | uniq -c


paj*_*ton 6

cat <filename> | sort | uniq -c
Run Code Online (Sandbox Code Playgroud)