Bash

如何顯示 uniq 大於 3 的行

  • January 24, 2017

我有一個文件,我應該在其中製作“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

我哪裡有錯誤?

如果要計算唯一行並僅顯示重複超過 3 次的行,可以使用以下-c選項uniq

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

過濾器awk輸出以顯示重複次數超過 3 的行,然後根據需要格式化輸出。

請注意,這要求文件已經排序,因為從您的文章中可以清楚地看出。

引用自:https://serverfault.com/questions/828237