Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion

Need some help to parse a nginx access.log file in bash
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Need some help to parse a nginx access.log file in bash

AmitzAmitz Member
edited January 2015 in Help

Dear all,

I would like to achieve the following with a bash one-liner or script:

  • Parse a standard nginx access.log
  • Print out a list of the total number of requests per unique IP, sorted in descending order.

Something like:

Unique IP                 Number of Requests
123.123.123.123           30,476
124.124.124.124           20,139
125.125.125.125           15,545
...                       ...

Is there any bash/awk/sed guru out there who may be able to help me with that? Any hint is highly appreciated!

Many thanks in advance & kind regards
-Amitz

P.S.:
I tried it with this:

FILE=/path/to/access.log;
for ip in `cat $FILE |cut -d ' ' -f 1 |sort |uniq`;
do { COUNT=`grep ^$ip $FILE |wc -l`;
if [[ "$COUNT" -gt "500" ]]; then echo "$COUNT:   $ip";
fi }; done

but I wonder if there is a more elegant way to do so...

Comments

  • Scratch it! :-)
    I found a nice one-liner that does the job for me!

    For others and future reference:

    cat /path/to/access.log |awk '{print $1}' |sort |uniq -c |sort -n |tail
    
    Thanked by 1rokok
Sign In or Register to comment.