如果您知道时间戳位于哪个字段,例如第二个单词,您可以使用 awk 通过调用来进行转换date:
stdbuf -oL tail -f ... |
awk -v timefield=2 '{
if($timefield ~ /[0-9].*Z/){
"date --date \"" $timefield "\"" | getline tod
sub($timefield,tod,$0)
}
print
}'
Run Code Online (Sandbox Code Playgroud)
您可能需要让 tail 不缓冲到管道,如上面所示stdbuf -oL。如果您不知道该字段,或者它发生了移动,您可以尝试匹配每个单词的时区模式:
awk '{
for(timefield = 1;timefield<=NF;timefield++)
if($timefield ~ /^[0-9].*T.*Z$/){
"date --date \"" $timefield "\"" | getline tod
sub($timefield,tod,$0)
}
print
}'
Run Code Online (Sandbox Code Playgroud)
这假设您的时间戳由空格分隔。
使用 的注释中的示例I2016-04-08T00:34:29.372Z]v3087,您可以使用 awk 的 提取时间戳substr(string,offset,length),因为它具有恒定的长度和偏移量:datestamp = substr($timefield, 2, 24)。如果您不想I在结果输出中保留初始代码和尾随代码,您最终会得到:
awk -v timefield=1 '{
if($timefield ~ /[0-9].*Z/){
datestamp = substr($timefield, 2, 24)
"date --date \"" datestamp "\"" | getline tod
sub($timefield,tod,$0)
}
print
}'
Run Code Online (Sandbox Code Playgroud)
如果您想保留额外的代码,请更改sub以替换日期戳字符串而不是整个字段,例如使用额外的空格:sub(datestamp," " tod " ",$0)。