1

The log file has the following date format and I was hoping that grep could extract the last 24hrs. I managed to get todays data via grep "22/10/2021" test.txt, but want to setup crontab to execute a script each day that simply selects the last 24hrs from the logfile and places it in the test.txt file.

[22/10/2021, 3:12:57 pm] [Kitchen ] Received MQTT: zigbee2mqtt/0x84 
[22/10/2021, 3:13:35 pm] [Son17] Received MQTT: zigbee2mqtt/0x00 
[22/10/2021, 3:13:42 pm] [Entrance ] Received MQTT: zigbee2mqtt/0x8
Ed Morton
  • 188,023
  • 17
  • 78
  • 185

1 Answers1

0

That's not a job for grep, it's a job for awk (with an assist from GNU date in this case):

awk -v tgt="$(date +'%F %T' --date='-24 hours')" -F'[][, /:]+' '
    {
        hr = $5 + ($8=="pm" && $5<12 ? 12 : 0)
        cur = sprintf("%4d-%02d-%02d %02d:%02d:%02d",$4,$3,$2,hr,$6,$7)
    }
    cur > tgt
' file

Add:

print tgt, cur, $0

to the script if you not sure what's happening and it should be pretty obvious.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185