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

后端 未结 2 1686
野趣味
野趣味 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: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
    

提交回复
热议问题