The problem with your approach is that you are looking for a pattern which contains [
and ]
. These have a special meaning in regular expressions, namely they are "character lists" and match any character enclosed in them. So, performing
grep '[com.java.Name abc]' logfile
would match any line containing any of the characters a
, b
, c
, e
, j
, m
, o
, v
, N
, the space, and the period .
, regardless of their location on the line (which likely matches every line of the log file).
You need to escape the [
and ]
, as in
grep -c '\[com.java.Name abc\]' logfile
or - as pointed out by @terdon - use the -F
flag:
grep -c -F '[com.java.Name abc]' logfile
If you want to look for occurences at a certain date, the mechanism depends. If you know the day, say 2020-06-14
, it could be as easy as stating
grep -c '^2020-06-14.*\[com.java.Name abc\]' logfile
If you want to search based on the full timestamp, that approach would only work if you knew the exact moment as it is formatted in the logfile, as in
grep -c '^2020-06-14 13:48:12,442.*\[com.java.Name abc\]' logfile
which is unlikely because then you probably wouldn't need to count the occurences in the first place. In that case, you could try to adapt some of the following answers: