I need to count messages per hour in my log file. Every log file line is preceded by the time stamp. Hence I am using following 'for' and 'grep' command to do this -
for i in `seq 0 23`
do egrep "$i:[0-9][0-9]:[0-9][0-9] <some_pattern>" filename | wc -l
done
This will give me number of messages per hour for 0 to 23.
However this does not work with single digit hour such as 5:23:32
because it is preceded by a white space. Then the grep would have to be -
egrep " $i:[0-9][0-9]:[0-9][0-9] <some_pattern>" filename | wc -l
If not it will incorrectly match lines starting with say 15:23:32
So how can I tell grep that a digit can be preceded by a space or start of the line only.