gpt4 book ai didi

linux - 如果时间戳大于一分钟则打印

转载 作者:行者123 更新时间:2023-12-01 22:59:34 26 4
gpt4 key购买 nike

我有包含 8 个字段的输入行。像这样:

Field1  Field2  Field3  Field4  Field5  Field6  Field7    Field8
name ID number stuff Jan15 ? 00:00:00 some command

其中一个字段 Field7 是一个时间戳,如 00:00:00我想“扫描”这第 7 个字段,看看时间是否大于一分钟,即第 7 个字段是否大于 00:01:00。

如果第 7 个字段更大,我想将字段 2 7 和 8 的值打印到文件中。我对 awk 的经验很少,但据我所知,这是我想使用的工具。

最佳答案

编辑 1:使用 ps 运行它命令使用它,如:

ps -ef | awk '
{
split($7,array,":")
tot_time=array[2]*60+array[3]
if(tot_time>60){
print $2,$7,$8
}
tot_time=""
delete array
}
'

还要涵盖进程运行时间为 1 小时且不到一分钟的 1 个边缘情况:) 尝试以下操作。
ps -ef | awk '
{
split($7,array,":")
tot_time=array[1]*3600+array[2]*60+array[3]
if(tot_time>60){
print $2,$7,$8
}
tot_time=""
delete array
}
'



你可以试试下面的。将第 7 列拆分为 3 个不同的部分(小时、分钟和秒),分隔符为 :然后从中计算分钟以检查其值是否大于 60。
awk '
{
split($7,array,":")
tot_time=array[2]*60+array[3]
if(tot_time>60){
print $2,$7,$8
}
tot_time=""
delete array
}
' Input_file



用 sample 测试:
cat Input_file
Field1 Field2 Field3 Field4 Field5 Field6 Field7 Field8
xxx xxx xxx xxx xxx xxx 00:01:01 xxx
xxx xxx xxx xxx xxx xxx 00:00:48 xxx

运行代码后,以下将是输出。
awk '
{
split($7,array,":")
tot_time=array[2]*60+array[3]
if(tot_time>60){
print $2,$7,$8
}
tot_time=""
delete array
}
' Input_file
xxx 00:01:01 xxx

关于linux - 如果时间戳大于一分钟则打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60034193/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com