繁体   English   中英

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

[英]print if timestamp is greater than a minute

我有包含 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 的经验很少,但据我所知,这是我想使用的工具。

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
}
'

还要涵盖进程运行时间为 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


用样品测试:

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

假设字段由空格或制表符分隔,如何:

awk '$7 > "00:01:00" {print $2, $7, $8}' file

输入

name1   ID1      number1  stuff   Jan15   ?       00:00:59  somecommand1
name2   ID2      number2  stuff   Jan15   ?       00:01:00  somecommand2
name3   ID3      number3  stuff   Jan15   ?       00:01:01  somecommand3
name4   ID4      number3  stuff   Jan15   ?       00:02:00  somecommand4

输出

ID3 00:01:01 somecommand3
ID4 00:02:00 somecommand4

awk 主要将两个字符串作为strings进行比较,除非两者都是数值或者一个是数字而另一个是数字字符串。
在这种情况下执行string comparison ,然后您可以直接比较 HH:MM:SS 表示中的时间字符串。

假设您的文件是空格/制表符分隔的,这应该可以解决问题:

awk '$7!~/^(00:01:00|00:00)/{print $2,$7,$8}' file > out_file

如果您有以下输入文件:

Field1  Field2  Field3  Field4  Field5  Field6  Field7    Field8
name    ID      number  stuff   Jan15   ?       00:00:14  somecommand
name2   ID2     number2 stuff   Jan15   ?       00:00:30  somecommand2
name3   ID3     number3 stuff   Jan15   ?       00:01:30  somecommand3
name4   ID4     number4 stuff   Jan15   ?       01:01:30  somecommand4
name5   ID5     number5 stuff   Jan15   ?       01:00:00  somecommand5
name6   ID6     number6 stuff   Jan15   ?       00:01:00  somecommand5

您可以使用以下awk命令

awk '$7 !~ /(^00:00|00:01:00)/ || NR==1 {print $2,$7,$8}' f.in | column -t

它将给出输出:

Field2  Field7    Field8
ID3     00:01:30  somecommand3
ID4     01:01:30  somecommand4
ID5     01:00:00  somecommand5

说明:

  • 基于正则表达式的方法
  • NR==1打印第一行(不需要的可以去掉这部分)
  • 该模式不是在00:00开始或不等于00:01:00 ,如果您将约束更改为大于或等于 1 分钟,则可以将其删除
  • 打印必填字段
  • column -t用于漂亮的输出,可以删除。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM