Search code examples
awkgrep

Count unique visits (by IP) from apache logs


I'm trying to calculate the number of unique visits on a URL from my apache logs.

Here is what I have so far:

grep -o 'THIS_IS_MY_URL' other_vhosts_access.log | wc -l

This is working, but I'm not filtering on unique IPs yet. Do you have any idea?

Thanks !


Solution

  • Those log files are most of the time in the following format:

    <IP> - - <TIMESTAMP> <METHOD> <URL> ....
    

    So instead of just using , we'll need some more:

    1. Remove the -o from grep so we're getting the whole line where the matching URL is found
    2. Then we can use cut -d' ' -f1 to get only a list of IP addresses [docs]
    3. Use the util uniq to filter it to only unique values [docs]
    4. Count the lines using wc -l
    grep 'SEARCH ME' file.log | cut -d' ' -f1 | uniq | wc -l