Grep and Count Unique String Occurance (after a string)

So I got assignment about how to get and count unique command occurance from my log. For example I had this kind of log

REQ 20161010 command=buy | title=soap | note=yes
REQ 20161010 command=loan | title=book | note=yes
RESP 20161010 command=buy | title=book | note=yes
REQ 20161009 command=loan | title=pen | note=yes
REQ 20161010 command=buy | title=ink | note=no

I need to get this kind of result

buy 3
loan 2

so, after researched it, it is actually easy. You can try this kind of command

Grep the string at any row with string “command=” between any string

grep -oh “\w*command=\w*” *

Count row that contain “string” in file.txt

grep -c string file.txt

First grep the word that contain string start with “command=” and followed by any string in buy.log. And then sort by the result. Lastly using uniq -c, count the unique occurance of the command=\w*

grep -oh “command=\w*” buy.log | sort | uniq -c

Other sample :

  1. to check and count by unique IP address that access your certain page from your access.log

less access.log | grep search.php | awk ‘{print $1}’ | sort | uniq -c


Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:


You are commenting using your account. Logout /  Ubah )

Foto Google+

You are commenting using your Google+ account. Logout /  Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout /  Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout /  Ubah )


Connecting to %s