1

I have a shell script which is grepping the results of a file then it calls sort -u to get the unique entries. Is there a way to have sort also tell me how many of each of those entries there are? So the output would be something like:

user1 - 50
user2 - 23
user3 - 40

etc..
4

3 回答 3

5

Use sort input | uniq -c. uniq does what -u does in sort -u, but also has the additional -c option for counting.

于 2013-07-22T17:45:10.030 回答
1

Grep 有一个 -c 开关来计算每个项目的出现次数。

grep -c needle haystack

将给出您可以根据需要排序的针数..

于 2013-07-22T17:53:03.163 回答
0

Given a sorted list, uniq -c will show the item, and how many. It will be the first column, so I will often do something like:

sort file.txt | uniq -c |sort -nr

The -n in the sort will parse numbers correctly, like 9 before 11 (though with the '-r', it will reverse the count, since I usually want the higher count lines first).

于 2013-07-22T17:46:09.283 回答