简体   繁体   English

将时间戳转换为就地文件中的纪元

[英]Convert timestamp to epoch in a file in-place

I have a tab delimited file with timestamp in third field which I need to change into epoch in bash. 我有一个制表符分隔文件,在第三个字段中有时间戳,我需要在bash中更改为epoch。

Sample Input: 样本输入:

xyz@gmail.com   SALE    2017-04-26 12:47:27     30.0    1       201704
xyz@gmail.com   SALE    2017-04-26 12:46:15     20.0    2       201704
xyz@gmail.com   PAYBACK 2017-04-18 08:02:31     95.0    3       201704
xyz@gmail.com   SEND    2017-04-18 08:00:37     4800.0  4       201704
xyz@gmail.com   SEND    2017-04-17 14:59:34     4900.0  5       201704

I tried awk 'BEGIN {IFS="\\t"} {$3=system("date -d \\""$3"\\" '+%s'");print}' file which gives the closest results but it displays epoch in one line then shows the record again in a newline with timestamp value as zero. 我试过awk 'BEGIN {IFS="\\t"} {$3=system("date -d \\""$3"\\" '+%s'");print}' file ,它给出了最接近的结果,但它显示了纪元然后在一行中再次在时间戳值为零的换行符中显示记录。 I require all in a single record with third field replaced. 我要求所有人都在一个记录中替换第三个字段。

With GNU awk: 使用GNU awk:

$ cat tst.awk
BEGIN { FS=OFS="\t" }
{
    $3 = mktime(gensub(/[-:]/," ","g",$3))
    print
}

$ awk -f tst.awk file
xyz@gmail.com   SALE    1493228847      30.0    1       201704
xyz@gmail.com   SALE    1493228775      20.0    2       201704
xyz@gmail.com   PAYBACK 1492520551      95.0    3       201704
xyz@gmail.com   SEND    1492520437      4800.0  4       201704
xyz@gmail.com   SEND    1492459174      4900.0  5       201704

With other awks: 与其他awks:

$ cat tst.awk
BEGIN { FS=OFS="\t" }
{
    cmd = "date -d \"" $3 "\" \047+%s\047"
    if ( (cmd | getline line) > 0 ) {
        $3 = line
    }
    close(cmd)
    print
}

$ awk -f tst.awk file
xyz@gmail.com   SALE    1493228847      30.0    1       201704
xyz@gmail.com   SALE    1493228775      20.0    2       201704
xyz@gmail.com   PAYBACK 1492520551      95.0    3       201704
xyz@gmail.com   SEND    1492520437      4800.0  4       201704
xyz@gmail.com   SEND    1492459174      4900.0  5       201704

wrt your script - there is no builtin awk variable named IFS , system returns the exit status of the last command run, not it's stdout, and you cannot include ' s in any ' -delimited script invoked from shell. 你的脚本 - 没有名为IFS内置awk变量, system返回最后一个命令运行的退出状态,而不是它的标准输出,并且你不能在从shell调用的任何' -delimited脚本中包含'

wrt wanting to do it "in-place", no UNIX editor REALLY does editing in-place but in GNU awk you can use -i inplace to avoid specifying the tmp file name yourself. 想要“就地”执行此操作,没有UNIX编辑器真的在就地编辑,但在GNU awk中你可以使用-i inplace来避免自己指定tmp文件名。 With any UNIX command, though, you can just do cmd file > tmp && mv tmp file . 但是,使用任何UNIX命令,您只需执行cmd file > tmp && mv tmp file

Note that this is one of the very few appropriate uses for getline - see http://awk.freeshell.org/AllAboutGetline for other valid uses and, most importantly, caveats and reasons not to use it unless absolutely necessary. 请注意,这是getline的极少数适当用途之一 - 请参阅http://awk.freeshell.org/AllAboutGetline了解其他有效用途,最重要的是,除非绝对必要,否则请注意不要使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM