Converting dates in AWK

后端 未结 3 1001
轻奢々
轻奢々 2020-12-05 14:32

I have a file containing many columns of text, including a timestamp along the lines of Fri Jan 02 18:23 and I need to convert that date into MM/DD/YYYY H

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-05 15:14

    If you're using gawk, you don't need the external date which can be expensive to call repeatedly:

    awk '
    BEGIN{
       m=split("Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec",d,"|")
       for(o=1;o<=m;o++){
          months[d[o]]=sprintf("%02d",o)
        }
    format = "%m/%d/%Y %H:%M"
    }
    {
    split($4,time,":")
    date = (strftime("%Y") " " months[$2] " " $3 " " time[1] " " time[2] " 0")
    print strftime(format, mktime(date))
    }'
    

    Thanks to ghostdog74 for the months array from this answer.

提交回复
热议问题