usual me
usual me

Reputation: 8778

Multiple counts in grep?

So I have a big log file where each line contains a date. I would like to count the number of lines containing each date.

I came up with an awful solution, consisting of manually typing each of the following commands:

grep -c "2014-01-01" big.log
grep -c "2014-01-02" big.log
grep -c "2014-01-03" big.log

I could also have written a small Python script, but that seems overkill. Is there a quicker / more elegant solution?

Upvotes: 2

Views: 247

Answers (3)

fedorqui
fedorqui

Reputation: 289675

You can maybe use of a regex and then uniq -c to count the results.

See an example:

$ cat a
2014-01-03 aaa
2014-01-03 aaa
2014-01-02 aaa
2014-01-01 aaa
2014-01-04 aaa
hello
2014-01-01 aaa

And let's look for all the 2014-01-0X, being X a digit, and count them:

$ grep -o "2014-01-0[0-9]" a | sort | uniq -c
      2 2014-01-01
      1 2014-01-02
      2 2014-01-03
      1 2014-01-04

Note piping to sort is needed to make uniq -c work properly. You can see more info about it in my answer to what is the meaning of delimiter in cut and why in this command it is sorting twice?.

Upvotes: 4

Mark Setchell
Mark Setchell

Reputation: 207455

Borrowing fedorqui's sample date file - thanks @fedorqui :-)

awk '/2014/{x[$1]++}  END{for (k in x) print x[k],k}' file
2 2014-01-01
1 2014-01-02
2 2014-01-03
1 2014-01-04

Upvotes: 3

smn_onrocks
smn_onrocks

Reputation: 1342

try this

 grep '2014-01-01' big.log |wc -l
 grep '2014-01-02' big.log |wc -l
 grep '2014-01-03' big.log |wc -l

Hope this will solve ur prob

Upvotes: 1

Related Questions