Peter Reavy

Bash one-liner to count occurrences in a log

Posted in Uncategorized by Peter Reavy on May 22, 2012

I’m assuming log.txt is the output of grepping for some kind of error in a log file.

Now I want to look at the occurrence times of these.

cat log.txt | cut -c 12-16 | sort | uniq -c | sed 's/^[ \t]*//' | tr -s ' ' ','

I’m cutting out the time column by character position. Uniq needs sorted input. Its output has leading whitespace which is removed by the sed expression. The tr then replaces the remaining spaces with commas because in this case I want a .CSV ready for Excel import. If you do import into Excel then make sure you set the time column as a time field before you do a histogram.

More sed fun here.