List grouped and sorted by count unique IPs (BASH)

There are two possible solutions to this. My attempt was as follows.

cat ./listofip |awk ‘{print $1}|sort|uniq -c|sort -n|more

This rendered an adequate result, but my output was in reverse order and still contained ports.

      1 10.0.2.31:11999
      1 127.0.0.1:32000
      1 127.0.0.1:47016
      1 127.0.0.1:47642
      1 127.0.0.1:47644
      1 127.0.0.1:47662
      1 127.0.0.1:47664
      1 127.0.0.1:47666
      1 127.0.0.1:47710
      1 127.0.0.1:47716
      1 127.0.0.1:47768
      1 127.0.0.1:47779
      1 127.0.0.1:49418
      1 ::ffff:10.0.2.18:61617
      1 ::ffff:10.0.2.19:52039
      1 ::ffff:10.0.2.27:35840
      1 ::ffff:10.0.3.19:35024
      1 ::ffff:10.0.3.22:51753
      1 ::ffff:10.0.3.25:55746
      1 ::ffff:10.0.3.25:56089
      1 ::ffff:10.0.3.28:51756
      1 ::ffff:10.0.4.26:45995
      1 ::ffff:127.0.0.1:3306
      1 ::ffff:127.0.0.1:42719
      1 ::ffff:52.0.22.114:55340
      2 127.0.0.1:8009
      8 10.0.2.31:3306
My second attempt was:
cat ./listofip | awk ‘ $0 ~ /^(::ffff:|[0-9|])/ { gsub(“::ffff:”,””,$0); print $0}’ | cut -d: -f1 | sort | uniq -c | sort -nr
This returned the result of:
    16 127.0.0.1
      9 10.0.2.31
      2 10.0.3.25
      1 52.0.22.114
      1 10.0.4.26
      1 10.0.3.28
      1 10.0.3.22
      1 10.0.3.19
      1 10.0.2.27
      1 10.0.2.19
      1 10.0.2.18
However, I could have written this in a shorter way.
awk -F: ‘{print $(NF-1)}’ /tmp/foo | sort | uniq -c | sort -rn
Which would have returned the same result, but required less functions.
Advertisements
List grouped and sorted by count unique IPs (BASH)