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

Jay*_*man 3 unix linux shell awk

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

Field1  Field2  Field3  Field4  Field5  Field6  Field7    Field8
name    ID      number  stuff   Jan15   ?       00:00:00  some command 
Run Code Online (Sandbox Code Playgroud)

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

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

Rav*_*h13 5

EDIT1:要使用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
}
'
Run Code Online (Sandbox Code Playgroud)

还要涵盖进程运行时间为 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
}
'
Run Code Online (Sandbox Code Playgroud)

你能不能试试以下。将第 7 列拆分为 3 个不同的部分(小时、分钟和秒),并使用分隔符 as:然后从中计算分钟以检查其值是否大于 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
Run Code Online (Sandbox Code Playgroud)

用样品测试:

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
Run Code Online (Sandbox Code Playgroud)

运行代码后,以下将是输出。

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
Run Code Online (Sandbox Code Playgroud)