如何显示 uniq 超过 3 的行

如何显示 uniq 超过 3 的行

我有一个文件,我应该在其中制作“uniq”并显示结果。我的文件:

178.57.66.225 fxsciaqulmlk
178.57.66.225 fxsciaqulmlk
178.57.66.225 fxsciaqulmlk
178.57.66.225 faaaaaa11111
178.57.66.215 terdsfsdfsdf
178.57.66.215 terdsfsdfsdf
178.57.66.215 terdsfsdfsdf
178.57.66.205 erdsfsdfsdf
178.57.66.205 erdsfsdfsdf
178.57.66.205 erdsfsdfsdf
178.57.66.205 erdsfsdfsdf
178.57.66.205 abcbbabab
178.57.66.205 abcbbabab
178.57.66.205 abcbbabab
178.56.66.225 fxsciulmla
178.56.66.225 fxsciulmla
178.56.66.225 fxsciulmla
178.57.67.225 faaaa0a1111

我的脚本:

for i in $(uniq -c /root/log | awk '{print $1}'); do
if [ $i -gt 2 ]; then
s=$(uniq -c /root/log | awk '$1 == '$i | awk '{print $3}')
ss=$(uniq -c /root/log | awk '$1 == '$i | awk '{print $2}')
echo "The bot is user $s with ip $ss"
fi
done

一切正常,但我的输出不正确:

The bot is user fxsciaqulmlk
terdsfsdfsdf
abcbbabab
fxsciulmla with ip 178.57.66.225
178.57.66.215
178.57.66.205
178.56.66.225
The bot is user fxsciaqulmlk
terdsfsdfsdf
abcbbabab
fxsciulmla with ip 178.57.66.225
178.57.66.215
178.57.66.205
178.56.66.225
The bot is user erdsfsdfsdf with ip 178.57.66.205
The bot is user fxsciaqulmlk
terdsfsdfsdf
abcbbabab
fxsciulmla with ip 178.57.66.225
178.57.66.215
178.57.66.205
178.56.66.225
The bot is user fxsciaqulmlk
terdsfsdfsdf
abcbbabab
fxsciulmla with ip 178.57.66.225
178.57.66.215
178.57.66.205
178.56.66.225

我花了两天时间做这件事,但我不明白我哪里犯了错误?请帮忙。

当我增加条件下的 uniq 数量时:

if [ $i -gt 2 ]; then

我有一个正常的日志:

The bot is user erdsfsdfsdf with ip 178.57.66.205

我哪里错了?

答案1

如果要计算唯一的行数并仅显示重复 3 次以上的行,则可以使用以下-c选项uniq

$ uniq -c /tmp/log | awk '{ if ($1 > 3)  print "The bot is user",$3,"with ip",$2 }'

awk输出进行调整以显示重复次数超过 3 次的行,然后根据需要格式化输出。

请注意,这要求文件已经排序,正如您的帖子中明确指出的那样。

相关内容