In Linux it's straightforward :
awk ‘ {print $1}’ logfile | sort | uniq -c | sort -nr | head -n 5
How can I transform the same logic into a Python function? Thanks.
In Linux it's straightforward :
awk ‘ {print $1}’ logfile | sort | uniq -c | sort -nr | head -n 5
How can I transform the same logic into a Python function? Thanks.
You can use subprocess module. In below link you have an answer for your question. https://stackoverflow.com/a/13332300/4257838
You can use collections.Counter
to count the occurrences of unique IPs and then sort and slice the resulting dict items:
from collections import Counter
from operator import itemgetter
for i, n in sorted(Counter(l.split()[0] for l in open('logfile').readlines()).items(), key=itemgetter(1), reverse=True)[:5]:
print(n, i)