【问题标题】:print if timestamp is greater than a minute如果时间戳大于一分钟,则打印
【发布时间】:2020-02-03 06:37:04
【问题描述】:

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

【问题讨论】:

  • 请发布更清晰的输入和预期输出示例/示例,并从您的示例中删除 xxx,使其更清晰。

标签: linux shell unix awk


【解决方案1】:

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 小时且小于 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 命令中,它似乎确实有效,只是没有打印到终端屏幕上,我预计至少有几个打印输出。谢谢,这帮助我更多地了解了 awk 可以做什么。
  • @JayWehrman,我刚刚添加了一个代码工作的测试样本,看起来它对我来说工作得很好,你能告诉我这里有什么不工作吗?
  • hmm 所以当我制作自己的输入文件并将其提供给 awk 时它可以工作,但是当我尝试将 echo ps -ef 输入到同一个 awk 脚本中时,我正在将 awks 结果写入文件是空的。
  • @JayWehrman,我添加了一个 EDIT1 解决方案,请您检查一下,让我知道这是否对您有帮助?
  • 非常感谢您的帮助!我的 bash 脚本中有 echo 之前的 ps
【解决方案2】:

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

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 表示中的时间字符串。

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      如果你有以下输入文件:

      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 用于漂亮的输出,可以删除。

      【讨论】:

        猜你喜欢
        • 2014-08-15
        • 1970-01-01
        • 2021-03-13
        • 2018-10-24
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多