为什么此命令不根据 uniq 计数进行排序?

MrD*_*Duk 2 grep sort uniq

我在日志中有类似于以下内容的行:

2015/11/02-07:55:39.735 INFO failed with ERR_AUTHORIZATION_REQUIRED.  (10.10.10.11:61618) is not a trusted source.
2015/11/02-07:55:40.515 INFO failed with ERR_AUTHORIZATION_REQUIRED.  (10.10.10.11:51836) is not a trusted source.
2015/11/02-07:55:39.735 INFO failed with ERR_AUTHORIZATION_REQUIRED.  (10.10.10.10:61615) is not a trusted source.
2015/11/02-07:55:40.515 INFO failed with ERR_AUTHORIZATION_REQUIRED.  (10.10.10.10:51876) is not a trusted source.
2015/11/02-07:55:39.735 INFO failed with ERR_AUTHORIZATION_REQUIRED.  (10.10.10.10:61614) is not a trusted source.
2015/11/02-07:55:39.735 INFO failed with ERR_AUTHORIZATION_REQUIRED.  (10.10.10.15:61614) is not a trusted source.
2015/11/02-07:55:39.735 INFO failed with ERR_AUTHORIZATION_REQUIRED.  (10.10.10.15:61618) is not a trusted source.
2015/11/02-07:55:39.735 INFO failed with ERR_AUTHORIZATION_REQUIRED.  (10.10.10.15:61613) is not a trusted source.
Run Code Online (Sandbox Code Playgroud)

所以,我尝试了以下命令来获取每个 uniq IP 的计数,排序:

grep ERR_AUTHORIZATION_REQUIRED file.log | awk '{print $6}' | cut -s -d ':' -f1 | tr -d '(' | sort | uniq -c
Run Code Online (Sandbox Code Playgroud)

我得到的输出类似于以下内容:

3 10.10.10.10
2 10.10.10.11
3 10.10.10.15
Run Code Online (Sandbox Code Playgroud)

所以这就像在应用之前对 IP 进行排序uniq -c(这在给定命令的情况下是有意义的),但是如果我交换uniqsort命令,每个 IP 都会打印1.

Bow*_*Red 6

uniq联机帮助页:

DESCRIPTION
     Discard all but one of successive identical lines from INPUT (or standard input), writing to OUTPUT (or standard output).
Run Code Online (Sandbox Code Playgroud)

这里的关键词是“成功”。它不会在流中的任何点搜索重复项,只会搜索紧随其后的那些。排序强制所有重复项彼此相邻,因此可以删除(并计算)它们。