How to filter values in a date range when the exact timestamps are not entered in the log


I want to takes value counts in a given date range from a log file. My log file is looks like this.


2022-01-01-10:01 AAA-passed
2022-01-01-11:05 AAA-passed
2022-01-01-12:01 AAA-passed
2022-01-01-13:05 AAA-passed
2022-01-02-12:01 AAA-failed
2022-01-03-13:05 AAA-failed

I have tried the following method to take the value counts in the given time range.


pass=$(awk '/^'$t1.*'/,/'$t2.*'/' values.log | grep -w "AAA-passed" | wc -l)
echo $pass

This method works only if exact timestamps have been entered in the log file. But if we give a time range which are not entered in the log file, this method does not work and not gives the answer,

for an example if we give


this not gives any answer, because these exact values for t1 and t2 are not entered in the log file. I tried lot of other methods also but nothing worked for me. Can someone help me to figure out this. Thanks in advance..!

Edit –

I found a relevant answer for this,

awk -v 'start=2018-04-12 14:44:00.000' -v end='2018-04-12 14:45:00.000' '
   /^[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2} / {
     inrange = $0 >= start && $0 <= end
   inrange' < your-file

This method work for me, but I dont hard code values for t1 and t2

t1=$(date -d "${dtd} -7 days" +'%Y-%m-%d-%R')
t2=$(date '+%Y-%m-%d-%R')

Result time format - 2022-07-05-12:15
Required time format - 2018-04-12 14:44:00.000 

so how can I edit the above expressions to get the date time in required time format.


@Fravadona answered the question you asked so you should accept their answer but this is too long to add as a comment and requires formatting so here it is – FYI in addition to your timestamp comparison, you don’t need pipes to grep and wc when you’re using awk:


    awk -v beg="$t1" -v end="$t2" '
        (beg <= $1) && ($1 <= end) && /AAA-passed/ { cnt++ }
        END { print cnt+0 }
    ' values.log
echo "$pass"

Answered By – Ed Morton

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published