Bash:使用awk从文件中获取的日期时间字符串获取纪元时间

Bash: Obtain epoch time from date time string taken from file using awk

首先,抱歉,标题可能不太好。其实真的想不出一个好的标题,但我会尽量在这里解释,所以就这样吧,

我有一个名为 timeInfo 的文件,其中包含以下格式的日期时间字符串,

2018-06-05 00:35:51 Controller shutdown process initiated
2018-06-05 05:32:22 Controller startup process initiated
...
...

现在我要做的是,我需要获取这个时间并将其转换为 EPOCH 并将其存储到临时文件 tempFile,我已经尝试过了到目前为止是...

//$file points to **timeInfo** file
echo `grep 'Controller startup' $file  | date -d "`awk '{ print , }'`" >> $TEMP_FILE`

使用它时出现以下错误,

command substitution: line 73: unexpected EOF while looking for matching `"'

然后我尝试了一种不同的方法并使用了以下代码,

echo `grep "Controller startup" $file  | awk '{print ,}' >> $TEMP_FILE`

有了这个,我得到了一个文件 tempFile,其中包含以下信息,

2018-06-06 00:35:31

2018-06-06 00:51:32

这看起来好多了,但我需要把它放在 EPOCH 中!有没有一种方法可以更改上面的脚本以在 tempFile 中以 EPOCH 格式保存日期时间字符串。

希望听到您的建议!谢谢

cat logfile|awk '{print(,)}' |xargs -n 1 -I_ date +'%s' --date=_

抓取文件。然后awk前两个字段。并使用 "xargs -n 1" 一次将一个数据传递给 date 命令并使用 %s 获取纪元。

这可能是你想要的,需要gawk

$ awk '{t= FS ; gsub(/[-:]/," ",t); print mktime(t)}' file

1528173351
1528191142

或者这个

$ awk '/Controller (startup|shutdown)/{t= FS ; 
                                       gsub(/[-:]/," ",t); 
                                       print mktime(t)}' file