【问题标题】:Date Matching lines without a date [closed]没有日期的日期匹配行[关闭]
【发布时间】:2021-12-03 16:58:02
【问题描述】:

好的,我有一个过滤购买日期的大日志文件。

ps6100-01:SP:12-Nov-2014 15:00:02 I am broken help me
ps6100-01:SP:12-Nov-2014 15:00:02 I am broken help me i got to much to say
so I continue on the next line of the file
ps6100-01:SP:12-Nov-2014 15:00:02 I am broken help me
ps6100-01:SP:12-Nov-2014 15:00:02 I am broken help me
ps6100-01:SP:11-Nov-2013 15:00:02 I am fine
ps6100-01:SP:11-Nov-2013 15:00:02 I am fine

我正在使用它来提取当年的事件

CurYear=`date | awk '{ print $6 }'` 
perl -ne "print if /-$CurYear/" $file

没有日期的行根本不会被拉出。所以如果运行我的声明,我想要这个。

ps6100-01:SP:12-Nov-2014 15:00:02 I am broken help me
ps6100-01:SP:12-Nov-2014 15:00:02 I am broken help me i got to much to say
so I continue on the next line of the file
ps6100-01:SP:12-Nov-2014 15:00:02 I am broken help me
ps6100-01:SP:12-Nov-2014 15:00:02 I am broken help me

但我得到了这个,有时它有几行,所以我在输出中遗漏了一些关键的东西。

ps6100-01:SP:12-Nov-2014 15:00:02 I am broken help me
ps6100-01:SP:12-Nov-2014 15:00:02 I am broken help me i got to much to say
ps6100-01:SP:12-Nov-2014 15:00:02 I am broken help me
ps6100-01:SP:12-Nov-2014 15:00:02 I am broken help me

【问题讨论】:

  • 请不要在图片中显示文字。它们不可搜索,不可复制粘贴,而且比需要的重得多。复制粘贴您问题中的文本和format it properly

标签: bash perl awk sed


【解决方案1】:

假设:

  • 包含年份的行始终采用以下格式:

xxxxxx:xx:DD-Mon-YYYY HH:MM:SS ....

示例输入数据:

$ cat logfile
ps6100-01:SP:11-Nov-2015 15:00:02 I am fine
ps6100-01:SP:12-Nov-2014 15:00:02 I am broken help me
ps6100-01:SP:12-Nov-2014 15:00:02 I am broken help me i got to much to say
        so I continue on the next line of the file
and then another line
ps6100-01:SP:12-Nov-2014 15:00:02 I am broken help me
ps6100-01:SP:12-Nov-2014 15:00:02 I am broken help me
ps6100-01:SP:11-Nov-2013 15:00:02 I am fine
ps6100-01:SP:11-Nov-2013 15:00:02 I am fine

一个awk想法:

CurYear=2014

awk -F':' -v cyear="${CurYear}" '                  # define input field separator as ":", pass in bash variable CurYear
NF>=5    { n=split($3,arr,"[-| ]")                 # split 3rd field on "-" and "<space>"
           printme = ( arr[3] == cyear ) ? 1 : 0   # if log file entry == CurYear then set printme flag else clear
         }
printme                                            # if printme flag == 1 then print current line
' logfile

这会生成:

ps6100-01:SP:12-Nov-2014 15:00:02 I am broken help me
ps6100-01:SP:12-Nov-2014 15:00:02 I am broken help me i got to much to say
        so I continue on the next line of the file
and then another line
ps6100-01:SP:12-Nov-2014 15:00:02 I am broken help me
ps6100-01:SP:12-Nov-2014 15:00:02 I am broken help me

【讨论】:

  • 我可以将任何没有我的日期的行附加到它上面的行吗?
  • 不确定我是否理解这个问题;如果此代码不符合您的要求,请使用更具代表性的数据集更新问题
【解决方案2】:

您可以为此使用 perl

这假设 ps6100 对所有有日期的行都是通用的,如果不是所有行都通用,您可能需要将其更改为其他正则表达式

perl -ne 'chomp; /$ENV{"CurYear"}/ && do {print "\n$_"; $x=4}; --$x > 0 && $_ !~ /ps6100/ && print' file.log

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    相关资源
    最近更新 更多