The best snippet I could come up with is the following:
fgrep http://t.co/ /var/www/logs/access.log | cut -d " " -f 1 | \
fgrep -f /dev/fd/0 /var/www/logs/access.log | cut -d " " -f 1 | sort | uniq -c
What does this do?
We first find unique IP-addresses of visits that have http://t.co/
in the log entry. (Notice that this will only count visits that came directly from the ref, but not those that stayed and browsed the site further.)
After having a list of IP-addresses that, at one point, were referred from a given URL, we pipe such list to another fgrep
through stdin
— /dev/fd/0
(a very inefficient alternative would have been xargs -n1 fgrep access.log -e
instead of fgrep -f /dev/fd/0 access.log
) for finding all hits from such addresses.
After the second fgrep
, we get the same set of IP-addresses that we had in the first step, but now they repeat according to the total number of requests -- now sort
, uniq -c
, done. :)