Awk to find lines within date range in a file with custom date format

后端 未结 2 1687
野趣味
野趣味 2021-01-16 07:06

I\'m trying to find all lines between a date range in a file. However dates are formatted in a non standard way. Is there a way for awk to read these? The log file is format

相关标签:
2条回答
  • 2021-01-16 07:28
    $ cat tst.awk
    {
        mthNr = (index("JanFebMarAprMayJunJulAugSepOctNovDec",$1)+2)/3
        date  = sprintf("%02d%02d", mthNr, $2)
    }
    (date >= from) && (date <= to)
    
    $ awk -v from='0101' -v to='0210' -f tst.awk file
    Jan  5 11:34:00 log messages here
    Jan 13 16:21:00 log messages here
    Feb  1 01:14:00 log messages here
    Feb 10 16:32:00 more messages
    

    Massage to suit...

    0 讨论(0)
  • 2021-01-16 07:42

    With awk. 0101 is January 1st and 0210 February 10th.

    awk -v start="0101" -v stop="0210" \
        'BEGIN{m["Jan"]="01"; m["Feb"]="02"; m["Mar"]="03"; m["Apr"]="04"}
        {original = $0; $1 = m[$1]; $2 = sprintf("%.2d", $2)}
        $1$2 >= start && $1$2 <= stop {print original}' file
    

    Output:

    Jan  5 11:34:00 log messages here
    Jan 13 16:21:00 log messages here
    Feb  1 01:14:00 log messages here
    Feb 10 16:32:00 more messages
    
    0 讨论(0)
提交回复
热议问题