[英]How to add "date" as column in a log file with Awk command
我正在尝试构建自己的硬件监视器,但我一直在尝试将时间戳放入日志文件中。
我正在使用“ps axu”来获取结果,并在将结果发送到文件之前尝试使用 awk 来过滤结果。
使用以下命令:
ps axu |awk 'NR==1 {print $0}; $3 > 0'
我得到这个结果:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
user+ 1421 0.1 0.9 304212 36384 tty2 Sl+ Oct27 4:11 /usr/lib/
user+ 1584 0.1 2.0 3626228 82152 ? Ssl Oct27 5:43 /usr/bin/
user+ 2096 0.7 14.6 4197076 587032 ? Sl Oct27 22:14 /usr/lib/
user+ 2248 0.5 8.4 3147328 340168 ? Sl Oct27 14:48 /usr/lib/
user+ 2272 1.0 14.6 3298264 587372 ? Sl Oct27 30:47 /usr/lib/
user+ 18658 0.1 0.0 12232 2468 pts/1 S+ Oct28 1:03 top
我想将其发送到一个新文件中,再添加一个带有如下时间戳的列:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND TIMESTAMP
user+ 1421 0.1 0.9 304212 36384 tty2 Sl+ Oct27 4:11 /usr/lib/ Thu 29 Oct 2020 09:00:42 AM EDT
user+ 1584 0.1 2.0 3626228 82152 ? Ssl Oct27 5:43 /usr/bin/ Thu 29 Oct 2020 09:00:42 AM EDT
user+ 2096 0.7 14.6 4197076 587032 ? Sl Oct27 22:14 /usr/lib/ Thu 29 Oct 2020 09:00:42 AM EDT
user+ 2248 0.5 8.4 3147328 340168 ? Sl Oct27 14:48 /usr/lib/ Thu 29 Oct 2020 09:00:42 AM EDT
user+ 2272 1.0 14.6 3298264 587372 ? Sl Oct27 30:47 /usr/lib/ Thu 29 Oct 2020 09:00:42 AM EDT
user+ 18658 0.1 0.0 12232 2468 pts/1 S+ Oct28 1:03 top Thu 29 Oct 2020 09:00:42 AM EDT
我尝试使用 awk 来执行此操作,但是在执行命令“date”并将其作为参数传递给 awk 时,我得到了一些奇怪的结果。
这会添加一个带有“0”的列作为结果:
ps axu |awk 'NR==1 {print $0}; $3 > 0'|awk '{print $0, system(date) }'
这将在新行而不是新列中添加时间戳:
ps axu |awk 'NR==1 {print $0}; $3 > 0'|awk -v var1=date '{print $0 system(var1) }'
这会添加一个带有“0”的列作为结果:
ps axu |awk 'NR==1 {print $0}; $3 > 0'|awk '{print $0, system(date) }'
AWK 的系统确实返回命令的(在这种情况下: date
)退出状态(在这种情况下: 0
即一切正常)。 如果您对命令的输出感兴趣,则system
不是正确的工具。
GNU AWK 有自己的时间函数,它允许你得到你想要的 - systime()
自纪元开始就给出第二个, strftime
允许你将其格式化为人类可读的形式,例如获得 YYYYMMDD HH:MM:SS 你可以做strftime("%Y%m%d %H:%M:%S", systime())
能否请你尝试下面写在GNU测试awk
,使用systime
和strftime
功能awk
。
ps axu |
awk '
NR==1 {print;next}
$3 > 0{
print $0,strftime(" %a %d %b %Y %H:%M:%S %p %Z", systime())
}'
说明:为上述解决方案添加详细说明。
ps axu | ##Running ps axu command to get the process in system. Sending its output as standard input to awk command.
awk ' ##Starting awk program from here.
NR==1 {print;next} ##If its header/first line then simply print it and move cursor to next line.
$3 > 0{ ##Checking condition if 3rd field is greater than 0 then do following.
print $0,strftime(" %a %d %b %Y %H:%M:%S %p %Z", systime()) ##Using systime function to get time in mentioned different options manner with strftime.
}'
%a --> The locale’s abbreviated weekday name.
%d --> The day of the month as a decimal number (01–31).
%b --> The locale’s abbreviated month name.
%Y --> The full year as a decimal number (e.g. 2011).
%H --> The hour (24-hour clock) as a decimal number (00–23).
%M --> The minute as a decimal number (00–59).
%S --> The second as a decimal number (00–60).
%p --> The locale’s equivalent of the AM/PM designations associated with a 12-hour clock.
%Z --> The time zone name or abbreviation; no characters if no time zone is determinable.
我有一个傻瓜,你可以使用它提供的 gettime 扩展。
ps axu | gawk -i gettime 'NR==1{print $0, "TIMESTAMP"} $3 > 0{print $0, getlocaltime()}'
否则你可以使用管道
ps axu | awk 'NR==1{print $0, "TIMESTAMP"} $3 > 0{"date"|getline d; print $0, d}'
最后一个没有awk的解决方案
paste <(ps axu) <(echo "TIMESTAMP"; yes `date`) | sed -n '/^\s/q;p'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.