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

Time:07-06

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

values.log 

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.

t1='2022-01-01-10:01'
t2='2022-01-03-13:05'

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

    t1='2022-01-01-10:00'
    t2='2022-01-03-14:00'

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.

CodePudding user response:

Your timestamp format is YYYY-mm-dd-HH:MM so you can directly use string comparisons:

t1=2022-01-01-10:01
t2=2022-01-03-13:05

awk -v start="$t1" -v end="$t2" 'start <= $1 && $1 <= end' values.log

CodePudding user response:

@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:

t1='2022-01-01-10:01'
t2='2022-01-03-13:05'

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