using awk to check between two dates

前端 未结 2 1587
刺人心
刺人心 2020-12-21 11:48

I have a file with multiple data structures in it like so:

eventTimestamp: 2010-03-23T07:56:19.166
result: Allowed
protocol: SMS
payload: RCOMM_SMS

eventTim         


        
2条回答
  •  有刺的猬
    2020-12-21 12:14

    The key observation is that you can compare your timestamps using alphanumeric comparisons and get the correct answer - that is the beauty of ISO 8601 notation.

    Thus, adapting your code slightly - and formatting to avoid scroll bars:

    awk 'BEGIN {
            FS  = "\n"
            RS  = ""
            OFS = ";"
            ORS = "\n"
            t1  = "2010-03-23T07:45:00"
            t2  = "2010-03-23T08:00:00"
            m1  = "eventTimestamp: " t1
            m2  = "eventTimestamp: " t2
            }
    $1 ~ /eventTimestamp:/ && $4 ~ /SMS-MO-FSM(-INFO)?$/ {
        if ($1 >= m1 && $1 <= m2) print $1, $2, $3, $4;
    }' "$@"
    

    Obviously, you could put this into a script file - you wouldn't want to type it often. And getting the date range entered accurately and conveniently is one of the hard parts. Note that I've adjusted the time range to match the data.

    When run on the sample data, it outputs one record:

    eventTimestamp: 2010-03-23T07:56:19.186;result: Allowed;protocol: SMS;payload: SMS-MO-FSM
    

提交回复
热议问题