我需要将unix时间戳值(在lof文件的第一个字段中)转换为特定的日期时间格式(%m /%d /%Y%H:%M:%S。%3N)
举个例子。我有一个看起来像这样的日志文件
@echo off
:CountSuccessfulLogins date
set "dt=%~1"
set "prefix= INFO ;servername;"
set "suffix= .*;Open Analysis;H5SeLhbEVUKA0ml-kFUw5-151708d6b3Y8Tz;/Metric/core/corePortal;Success;151708fdfgY"
for /f %%N in ('findstr /rc:"^%prefix%%dt%%suffix%$" test.log ^| find /c /v ""') do set cnt=%%N
echo %cnt% Successful Logins on %dt%
exit /b
我需要它看起来像这样
"1456316486","1000","11678","0.00","0.00","0.00","0.00","1","0.00","0.00","1792364","652108","8.02","0.00","0.00","0.00","0","remmina"
能够将其导入另一个工具。
P.S。我知道有一些例子如何做到这一点,但作为一个awk n00b,我无法让它完全按照我的意愿去做。
感谢您的帮助
答案 0 :(得分:2)
我用$1+0
尝试了mpez0优雅的解决方案,但对我来说它没有用。我必须在strftime之前从"
明确删除$1
,所以我想出了GNU-Awk 4.0.1的这个解决方案:
awk -v FS=, -v OFS=, '{gsub(/"/,"",$1 ); $1 = strftime("\"%D %T.000\"",$1); print; }' file
%D
表示%m/%d/%y
,%T
表示%H:%M:%S
.000
-v
设置输入字段分隔符和输出字段分隔符,以使用,
答案 1 :(得分:1)
使用GNU awk for strftime:
$ awk 'BEGIN{FS=OFS="\""} {$2=strftime("%D %T.000")} 1' file
"02/26/16 07:53:29.000","1000","11678","0.00","0.00","0.00","0.00","1","0.00","0.00","1792364","652108","8.02","0.00","0.00","0.00","0","remmina"
答案 2 :(得分:0)
如果您使用的是最新版本的awk,其时间戳扩展名超过了原始版本(您很可能),那么
timestr = strftime("%m /%d /%Y%T.000",$ 1 + 0)
应该给出你想要的字符串。您需要+0才能将数字字符串转换为数字。