使用gawk从文件中的人类可读时间转换为unix时间?

时间:2015-07-31 23:42:19

标签: bash shell awk gawk

我是gawk的新手。基于this线程,我已经做了一个gawk函数来转换日期时间,例如" 07,JUN,2015,06,PM"到unix时间(毫秒):

$ cat tst.awk
function cvttime(t, a) {
    split(t,a,/[,: ]+/)

    # fa0,07,DEC,2014,10,AM,862.209018
    #  =>
    #    a[2] = "07"                date
    #    a[3] = "DEC"               month
    #    a[4] = "2014"              year
    #    a[5] = "06"                time
    #    a[6] = "AM"                AM/PM

    if ( (a[6] == "PM") && (a[5] < 12) ) {
        a[5] += 12
    }

    match("JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC",a[3])
    a[3] = (RSTART+2)/3

    return( a[1]","mktime(a[4]" "a[3]" "a[2]" "a[5]" 00 0")"000,"a[7])
}

BEGIN {
    mdt = "fa0,07,DEC,2014,10,AM,862.209018"    
    ms = cvttime(mdt)
    print ms
}

在终端中,以下命令为我提供了正确的unix时间:

$ TZ=UTC gawk -f tst.awk

返回:

fa0,1417946400000,862.209018

现在,我有一个文件&#34; input.csv&#34;包含:

aa1,07,DEC,2014,06,AM,282.485988
ac3,07,DEC,2014,07,AM,97.6757181
ef3,07,DEC,2014,08,AM,112.816554
ag3,07,DEC,2014,09,AM,101.479961
fa0,07,DEC,2014,10,AM,862.209018

我应该如何修改gawk函数和shell命令以传入&#34; input.csv&#34;并生成&#34; output.csv&#34;含有:

aa1,1417932000000,282.485988
ac3,1417935600000,97.6757181
ef3,1417939200000,112.816554
ag3,1417942800000,101.479961
fa0,1417946400000,862.209018

提前致谢!

2 个答案:

答案 0 :(得分:1)

您可以使用以下内容:

awk -F, '{"date -d "$3"\" \""$2"\" \""$5"\" \""$6"\" \""$4" '+%s'" | getline d; print $1","d"000,"$NF""}' input.csv > output.csv

awk -F, '{"date -u -d "$3"\" \""$2"\" \""$5"\" \""$6"\" \""$4" '+%s'" | getline d; print $1","d"000,"$NF""}' input.csv > output.csv

如果您想从日期命令

设置标志-u

-u, --utc, --universal               打印或设置协调世界时

答案 1 :(得分:0)

这很令人困惑,因为您的输入时间与输出时间不匹配, 但我认为这样做你想要的:

BEGIN {
  FS = OFS = ","
}
{
  # fix year
  $4 += 2000
  # fix month
  match("JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC", $3)
  $3 = (RSTART + 2) / 3
  # fix hour
  if ($6 == "PM" && $5 < 12)
    $5 += 12
  print $1, mktime($4 " " $3 " " $2 " " $5 " 0 0") * 1000, $NF
}