0

I have a file, where i should make "uniq" and show result. My file:

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

My script:

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

All ok, but my output is not correct:

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

I am making it during 2 days, and i can't understand where i make mistake ? Please help.

When, i increase number of uniq in condition:

if [ $i -gt 2 ]; then

I have a normal log:

The bot is user erdsfsdfsdf with ip 178.57.66.205

Where i have mistake ?

1 Answer 1

1

If you want to count the unique lines and show only the ones which repeated more than 3 times, you can use -c option of uniq like:

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

The awk fitlers the output to show lines with more than 3 repetitions, and then formats the output as desired.

Please, note that this requires the file to be already sorted as it is clear from your post.

1
  • thanks. i don't think, how to make it only with awk
    – Valeriu
    Jan 24, 2017 at 10:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .