Awk search Logs based on current date

Dear team,

Here is the log files which has event based on date and time. I need to search for events occured today only. The date format is YYMMDD only.

indent preformatted text by 4 spaces
awk '{a[++i]=$0;}END{for (j=1;j<NR;j++) {if (a[j]~200322) {for (y=0;y<7;y++) print a[j+y]}}}' ALLIP_HC_Log

but its not working when passing date using

awk '{a[++i]=$0;}END{for (j=1;j<NR;j++) {if (a[j]~$date) {for (y=0;y<7;y++) print a[j+y]}}}' ALLIP_HC_Log

also the date format is yyymmdd.. I tried to convert using below

newdate = $date | awk '{print substr($0,3,2) substr($0,5,2) substr($0,7,2)}'

awk '{a[++i]=$0;}END{for (j=1;j<NR;j++) {if (a[j]~$newdate) {for (y=0;y<7;y++) print a[j+y]}}}' ALLIP_HC_Log

It seems not working.

Log has event as below

A2/SW-DEV "AA1J_1.9" 119 200323   0345

DEST          SPIDNT    REASON
2-1111        121GP21   DEST UNREACHABLE


A2/SW-DEV "AA1J_1.9" 119 200309   0345
BLADE
DEST          SPIDNT    REASON
2-1133        GP21   DEST UNREACHABLE


A2/SW-DEV "HR1J_1.9_IPA01" 119 200309   0345
BLADE
SOURCE CP 08
2-1122        GP21   DEST UNREACHABLE

Really appreciate for help..

1 Like

Greetings and welcome to the forum!

As many seem to be busy here, I suppose you will have to make a little more effort in order to find rapid help, like giving you OS and its version, the shell you use, and in this case

It seems not working. is a bit light... In what way? the best is to give the output you got and explain what you see isn't right, or what was expected

All the best

1 Like

Yes, the new site is just getting 'moving' so some replies from our Team will be slower than before.

1 Like

Dear team,

Here is the version of my server "Linux version 4.4.121-92.98.1.164"
Not working I mean no result..

$ awk '{a[++i]=$0;}END{date="date +%Y%m%d";for (j=1;j<NR;j++) {if (a[j]~"$date") {for (y=0;y<7;y++) print a[j+y]}}}' ALLIP_HC_Log

Dear team,

I tried the below awk expression as well. getting error.

$ awk '{a[++i]=$0;}END{for (j=1;j<NR;j++) { date="$(date +'%Y%m%d')" | getline date }{if (a[j]~date) {for (y=0;y<7;y++) print a[j+y]}}}' ALLIP_HC_Log

sh: 20200323: command not found

What is it you want? as I have no time now to go through awk stuff, not my expertise...

you could try to write a script that reads line by line, with a line counter...

when a line matches your need ( the date ) it reads all the stanza to a output and continues to the next line ( yes it means a loop )

Unless an awk guru decides to come in

@qshaan, Thank you for sharing your efforts in form of code here. But to me your requirement is still not clear. Could you please post sample of Input and sample of expected output and let us know then.

Thanks,
R. Singh

1 Like

Thanks Ravinder.. The requirement is to filter events occured on current date from logfile.. The date format in logfile is yymmdd.. The below code seems to be working perfectly.. If u can any other suggestion . Will really appreciate


    $ awk -v date="$(date +'%g%m%d')"  '{a[++i]=$0;}END{for (j=1;j<NR;j++) {if (a[j]~date) {for (y=0;y<7;y++) print a[j+y]}}}' ALLIP_HC_Log
    A2/SW-DEV "AA1J_1.9_IPA01" 009 200323   0341
    BLADE
    SOURCE CP 07
    SUA SS7 DESTINATION INACCESSIBLE

    DEST          SPIDNT    REASON
    2-1111        SM22   DEST UNREACHABLE
    A2/SW-DEV "AA1J_1.9_IPA01" 012 200323   0341
    BLADE
    SOURCE CP 07
    SUA SS7 DESTINATION INACCESSIBLE

    DEST          SPIDNT    REASON
    2-1134        D61A   DEST UNREACHABLE

1 Like

how about:

awk -v d=$(date +'%g%m%d') '$0~d' RS= myFile

4 Likes