Counting occurrences of a word matching a regex

This is a brief code snippet showing how to use command line tools to count the occurences of words matching a regex. In the following example, I am trying to list all the unique hits on the urls from the apache access log matching “/chart”.

I am using the GNU version of sed (the Linux and CygWin version), so substitute “-r” for “-E” if using the BSD version (as on OS X).

tail -80000 /var/log/apache2/access_log > tail.txt
egrep "POST /chart/" tail.txt \
|sed -r -n 's/.*chart\/(get[a-zA-Z]+).*/\1/p' \
| sort > sortert

for word in $(uniq < sortert); do
/bin/echo -n "$word : ";
grep $word sortert |wc -l;
done \
| sed -r -n 's/([a-zA-Z]+) : (.+)/\2\t\1/p' \
|sort -g

Legg igjen et svar

Fyll inn i feltene under, eller klikk på et ikon for å logge inn:

WordPress.com-logo

Du kommenterer med bruk av din WordPress.com konto. Log Out / Endre )

Twitter picture

Du kommenterer med bruk av din Twitter konto. Log Out / Endre )

Facebookbilde

Du kommenterer med bruk av din Facebook konto. Log Out / Endre )

Kobler til %s

Følg med

Få nye innlegg levert til din innboks.

Bli med 74 andre følgere