如何使用AWK格式化字符串日期(使用文本和毫秒)

Dou*_*ble 4 unix bash shell awk

我正在研究一个解析数百万行文本的AWK脚本.每行包含(除其他外)表格上的日期和时间:

16-FEB-2008 14:17:59.994669
Run Code Online (Sandbox Code Playgroud)

我需要将其转换为以下形式

20080216141759994669000
Run Code Online (Sandbox Code Playgroud)

如果可能的话,我希望避免将月份从文本手动转换为数值.在bash中我可以简单地执行以下命令来获得所需的结果:

date -d "16-FEB-2008 14:17:59.994669" +"%Y%m%d%H%M%S%N"
Run Code Online (Sandbox Code Playgroud)

我试过在AWK中调用这个命令,但我无法弄清楚怎么做.我想知道

  1. 是否可以单独使用AWK实现?
  2. 如何在AWK脚本文件中使用这样的命令?

提前致谢

dav*_*085 5

将月份名称转换为awk中的数字很容易,只要您不需要(额外)验证date"免费" ,重新格式化也是如此:

$ echo this 16-FEB-2008 14:17:59.994669 that \
> | awk '{ split($2,d,"-"); split($3,t,"[:.]"); 
    m=sprintf("%02d",index("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC",d[2])/4+1);
    print $1,d[3] m d[1] t[1] t[2] t[3] t[4] "000",$4 }'
this 20080216141759994669000 that
$ # or can put the script in a file and use with awk -f
$ # or the whole thing in a shebang file like #!/bin/awk -f
Run Code Online (Sandbox Code Playgroud)

这不会比运行的代码date长得多,而且对于"数百万行"来说效率要高得多.