How do you compare a field in a file with the current timestamp and print larger and less important data?

advertisements

How do I compare current timestamp and a field of a file and print the matched and unmatched data. I have 2 columns in a file (see below)

oac.bat 09:09
klm.txt 9:00

I want to compare the timestamp(2nd column) with current time say suppose(10:00) and print the output as follows.

At 10:00

greater.txt

xyz.txt 10:32
mnp.csv 23:54

Lesser.txt

oac.bat 09:09
klm.txt 9:00

Could anyone help me on this please ?

I used awk $0 > "10:00", which gives me only 2nd column details but I want both the column details and I am taking timestamp from system directly from system with a variable like

d=`date +%H:%M`


With GNU awk you can just use it's builtin time functions:

awk 'BEGIN{now = strftime("%H:%M")} {
    split($NF,t,/:/)
    cur=sprintf("%02d:%02d",t[1],t[2])
    print > ((cur > now ? "greater" : "lesser") ".txt")
}' file

With other awks just set now using -v and date up front, e.g.:

awk -v now="$(date +"%H:%M")" '{
    split($NF,t,/:/)
    cur = sprintf("%02d:%02d",t[1],t[2])
    print > ((cur > now ? "greater" : "lesser") ".txt")
}' file

The above is untested since you didn't provide input/output we could test against.