解析日志文件,显示特定日期之间的日志

时间:2020-04-27 13:23:10

标签: linux shell logging

我想解析一个日志文件并提取两个特定日期之间的行...我已经阅读了很多关于此的页面,但是我的问题是我的日志文件具有特定的日期类型:

INFO - 27/04/2020 15:00:16 400 - [infoinfoinfo ] - [infoinfoinfo] - [infoinfoinfo]

这是我的日志文件中一行的示例。仅凭做个傻事(我仍然不知道怎么做),我看不出我的问题有任何问题!看看我的日期格式,您是否看到解决方案?

提前谢谢!

1 个答案:

答案 0 :(得分:0)

您应该将日期重新格式化为yyyy / mm / dd以便进行比较。

您可以创建log.awk文件

{
    # create a command that will convert the date from dd/mm/yyyy to yyyy/dd/mm
    # and apply it on third field
    cmd = "echo "$3" | sed 's-\\(.*\\)/\\(.*\\)/\\(.*\\)-\\3/\\2/\\1-'"

    # execute sed and store result in datef
    cmd | getline datef
    close(cmd)

    # filter on date
    if (datef>=datemin && datef<=datemax)
        print $0
}

并以此方式调用程序(此处仅在2020年4月进行过滤):

 awk -f log.awk -vdatemin=2020/04/01 -vdatemax=2020/04/30 log.log

您还可以在您的约会中添加时间(我没有测试过):

{
    # create a command that will convert the date from dd/mm/yyyy to yyyy/dd/mm
    # and apply it on third field
    cmd = "echo "$3" | sed 's-\\(.*\\)/\\(.*\\)/\\(.*\\)-\\3/\\2/\\1-'"

    # execute sed and store result in datef
    cmd | getline datef
    close(cmd)

    datetime = datef ":" $4

    # filter on date
    if (datetime >=datemin && datetime tef<=datemax)
        print $0
}

并以这种方式调用您的程序:

 awk -f log.awk -vdatemin=2020/04/01:12:00:10 -vdatemax=2020/04/30:15:00:00 log.log