wikipedia

Support Wikipedia

Friday, January 30, 2009

Search and grouping results in command line unix

Ever wanted to extract some metrics from log files and group the results. Also print the count for each group. All this without using a loop statement!!!
Just using command line. Here is how it can be done.

In the results, the first number is the count and the second is the group.

For starters here is a bit of explanation...The grep is looking for the string Failed to connect in service.log file. sed is used to make a group. In this case it is the station number. Then the results are sorted, finally uniq gives the unique rows and the -c gives us the count for each group.
BTW this was done on Ubuntu.

$ grep 'Failed to connect' service.log | sed 's/.*Station :\(.*\)/\1/' | sort | uniq -c
count
station
  69    10 - Host : 123.456.103.106 -  Failed to connect - Error :java.net.ConnectException: Connection timed out: connect
356 11 - Host : 123.456.103.105 - Failed to connect - Error :java.net.ConnectException: Connection timed out: connect
24 1 - Host : 123.456.103.107 - Failed to connect - Error :java.net.ConnectException: Connection timed out: connect
24 2 - Host : 123.456.103.98 - Failed to connect - Error :java.net.ConnectException: Connection timed out: connect
23 3 - Host : 123.456.103.99 - Failed to connect - Error :java.net.ConnectException: Connection timed out: connect
24 4 - Host : 123.456.103.100 - Failed to connect - Error :java.net.ConnectException: Connection timed out: connect
25 5 - Host : 123.456.103.101 - Failed to connect - Error :java.net.ConnectException: Connection timed out: connect
25 6 - Host : 123.456.103.102 - Failed to connect - Error :java.net.ConnectException: Connection timed out: connect
23 7 - Host : 123.456.103.97 - Failed to connect - Error :java.net.ConnectException: Connection timed out: connect
34 8 - Host : 123.456.103.104 - Failed to connect - Error :java.net.ConnectException: Connection timed out: connect
28 9 - Host : 123.456.103.103 - Failed to connect - Error :java.net.ConnectException: Connection timed out: connect

No comments:

Post a Comment