【问题标题】:Sed to extract with log between to given dateSed 提取与给定日期之间的日志
【发布时间】:2015-04-06 14:32:26
【问题描述】:

我正在尝试提取两个给定日期的日志。如果我指定像这样 Apr 02 15:21:28 这样的日期,代码工作正常,我的意思是如果知道准确的分钟和秒的时间,但是代码会因为我传递这样的值而失败 从 Apr 02 15* 到 Apr 04 15* 这里 15 是小时,实际上我想制作用户只需要添加日期和时间的脚本(仅以小时为单位,没有分钟或秒)

> #!/bin/bash
read -p " enter the App name : " app
file="/logs/$app/$app.log"
read -p " Enter the Date in this Format --'10 Jan 20 or Jan 10 20' : " first
read -p " Enter the End time of logs : " end

if [ -f "$file" ]
then
    if grep -q "$first" "$file"; then
        final_first=$first
    fi
    if grep -q "$end" "$file"; then
        final_end=$end
    fi

        sed -n " /$final_first/,/$final_end/ "p $file >$app.txt

else
        echo "$app.log not found, Please check correct log name in deployer"
fi

样本数据:

Apr 07 12:39:15 DEBUG [http-0.0.0.0-8089-21] model.DSSAuthorizationModel - pathInfo : /about-ses 
Apr 07 12:39:15 DEBUG [http-0.0.0.0-8089-21] servlet.CasperServlet - Request about to be serviced by model: com.ge.oilandgas.sts.model.SessionValidModel 

【问题讨论】:

  • 尝试将值作为Apr 02 15 传递,不带星号。请注意,这只有在日志文件中有匹配条目时才有效——如果 4 月 2 日 15:00 到 16:00 之间没有发生任何事情,则范围将永远不会匹配。
  • @Wintermute 是的,你是对的,我刚刚通过了 15 年 4 月 2 日和 15 年 4 月 4 日,这里 sed 找到了第一个日期,但没有找到 15 年 4 月 4 日,因此他提取了所有行,直到日志文件结束,即 Apr 06 ...你能告诉任何可以很好替代这个的代码吗,我的意思是如果没有找到 15 年 4 月 4 日,至少他可以在 4 月 4 日停止,不会超过 4 月 5 日
  • 您需要一个知道01 May 03 晚于30 Apr 22 的工具,而sed 并不那么聪明。我会使用 Perl,但您可能更喜欢 shell 脚本。
  • 从日志文件中包含一些示例行,以便我们查看时间戳的确切格式。
  • @glennjackman 我更新了我的代码,如果有的话,我想在这做什么.. 如果条件失败,它应该将空值放入 sed final first 和 final end 变量,并且 sed 命令将不起作用这个案例。

标签: linux shell variables if-statement scripting


【解决方案1】:

我会使用具有内置日期时间解析或易于包含的模块的语言。比如perl

first="Apr 07 12" 
end="Apr 08 00"
perl -MTime::Piece -sane '
    BEGIN {
        $first_ts = Time::Piece->strptime($first, "%b %d %H")->epoch;
        $end_ts = Time::Piece->strptime($end, "%b %d %H")->epoch;
    }
    $ts = Time::Piece->strptime(join(" ", @F[0..2]), "%b %d %T")->epoch;
    print if $first_ts <= $ts and $ts <= $end_ts;
' -- -first="$first" -end="$end" <<END

Apr 07 11:39:15 DEBUG [http-0.0.0.0-8089-21] model.DSSAuthorizationModel - pathInfo : /about-ses 
Apr 07 12:00:00 DEBUG [http-0.0.0.0-8089-21] model.DSSAuthorizationModel - pathInfo : /about-ses 
Apr 07 12:39:15 DEBUG [http-0.0.0.0-8089-21] model.DSSAuthorizationModel - pathInfo : /about-ses 
Apr 07 12:39:15 DEBUG [http-0.0.0.0-8089-21] servlet.CasperServlet - Request about to be serviced by model: com.ge.oilandgas.sts.model.SessionValidModel 
Apr 07 23:59:59 DEBUG [http-0.0.0.0-8089-21] servlet.CasperServlet - Request about to be serviced by model: com.ge.oilandgas.sts.model.SessionValidModel 
Apr 08 00:00:01 DEBUG [http-0.0.0.0-8089-21] servlet.CasperServlet - Request about to be serviced by model: com.ge.oilandgas.sts.model.SessionValidModel 
END

输出

Apr 07 12:00:00 DEBUG [http-0.0.0.0-8089-21] model.DSSAuthorizationModel - pathInfo : /about-ses 
Apr 07 12:39:15 DEBUG [http-0.0.0.0-8089-21] model.DSSAuthorizationModel - pathInfo : /about-ses 
Apr 07 12:39:15 DEBUG [http-0.0.0.0-8089-21] servlet.CasperServlet - Request about to be serviced by model: com.ge.oilandgas.sts.model.SessionValidModel 
Apr 07 23:59:59 DEBUG [http-0.0.0.0-8089-21] servlet.CasperServlet - Request about to be serviced by model: com.ge.oilandgas.sts.model.SessionValidModel 

鉴于您的代码,我将进行以下更改:

if ! [ -f "$file" ]; then
    echo "$app.log not found, Please check correct log name in deployer"
    exit 1
fi

grep -q "$first" "$file" && final_first="/$first/" || final_first='1'
grep -q "$end"   "$file" && final_end="/$end/"     || final_end='$'

sed -n "${final_first},${final_end}p" "$file" >"$app.txt"

这提供了 sed 范围、第一行和最后一行的默认地址。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多