将 apache 日志日期格式转换为 bash 中的纪元

Convert apache log date format to epoch in bash

我的目标是将 "12/Nov/2015:23:28:22" 格式的 apache 日志中的日期转换为纪元格式。这可以使用 date 命令完成,还是我需要解析和提取信息?

我的 date 命令似乎希望在日期部分之间使用 - 而不是 /,并且在时间部分之间用 space 分隔。所以我用 sed 像这样进行转换:

date -d "$(echo '12/Nov/2015:23:28:22' | sed -e 's,/,-,g' -e 's,:, ,')" +"%s"

您可以使用包 dateutils:

dateutils.strptime -i "%Y%m%dT%H%M%S" -f "%d.%m.%Y %H:%M:%S" "20170411T172238"

其中 -i 是您的输入格式,-f 是输出格式

虽然@Matthias Leuffen 的 很有前途(输入和输出格式错误但 w/e),但对我来说 -S 选项 dateutils.strptime根本不起作用,它没有发出日志的其余部分,只是打印了一堆时间戳,一个接一个,无穷无尽的数字流,所以我寻找了另一个解决方案。

https://groups.google.com/d/msg/comp.lang.awk/FXrz0VIvz4A/hhjQuu9gx3YJ adds strptime support to awk (to gawk 4, to be more precise). So you can use https://serverfault.com/a/253232/64874把你的log改成#分开然后

awk -l ./strptime.so -F'#' '{print strftime("%s", strptime(, "%d/%b/%Y:%H:%M:%S %Z")) "#" "#" }'

根据您的情况更改