【问题标题】:shell script for extracting specfic date log用于提取特定数据日志的 shell 脚本
【发布时间】:2015-03-12 15:01:45
【问题描述】:

我必须从任何应用程序日志中提取特定日期的日志。我们通常做什么

  1. vi app.log
  2. /日期(搜索)
  3. .=(找到日期的位置)
  4. G(最后一行)
  5. first_line,Last_line w filname.log(生成特定日期的日志文件)

我做了小脚本,但我认为我的逻辑不正确:

#!/bin/bash
read -p " enter the App name" : app
read =p " enter the Date in this Format 10 Jan 20:01:00" : date
FILE=/logs/$app.log

# Check if $app exists or not
if test ! -f "$app" /logs
then
    echo "Error - $app not found."
else

    First_line=$(cat $FILE | grep $date | .=)
    Second_line=$(tail -1 $FILE | .=)
    vi $FILE | $First_line, $Second_line w $app.txt
fi

【问题讨论】:

  • 1. read =p " 以这种格式输入日期 10 Jan 20:01:00" : date ...... 2 First_line=$(cat $FILE | grep $date | .=)
  • 在sed 或类似的情况下使用vim 会使工作变得不必要地困难。例如,sed -n '/2015-01-02/,$p' app.log 将打印从包含 2015 年 1 月 2 日的第一行到文件末尾的所有行。如果您的日期格式不同,请相应地修改搜索模式。请注意,如果您具体了解时间,您可能永远不会有记录(例如,如果您搜索 20:01:00,但日志记录从 20:00:59 到 20:01:01(没有提及20:01:00),那么您将什么也看不到。为此,您需要“如果日期大于开始日期”,但这比较棘手。

标签: linux bash shell date scripting


【解决方案1】:

行中有错误

if test ! -f "$app" /logs

你用空格分隔“$app”和/logs; test -f 需要一个参数,所以应该是:

if test ! -f "$app"/logs

要获取从给定日期到文件当前结尾的日志,您应该使用sed(意思是“流编辑器”),它专为文件的非交互式编辑而设计:

sed -n '/'$date'/,$ p' $app.log >$app.txt

参数-n是只输出匹配的,而不是整个文件。

这里我告诉sed 提取两个正则表达式之间的所有内容,第一个是给定的$date 参数,第二个是“文件结尾”。您可以轻松更改此设置以提取特定日期或其他任何时间的所有日志。

由于某些事情似乎不清楚,我正在添加一个完整的示例: 我有一个 apache 日志文件,其中记录的日期类似于 [01/Apr/2015:22:31:21 +0200]。

所以我可以这样做:

export date="01\/Apr\/2015"

重要的是要注意我在这里转义了斜杠,因为它们正在进入正则表达式。这也必须处理正则表达式中具有特殊含义的所有内容。

那我可以做:

sed -n '/'$date'/,$ p' myaccess.log

我记录了 4 月 1 日的所有内容,直到文件结束。

【讨论】:

  • 不我想要从第一行到最后一行的日志..就像 3 月 12 日的日志.. 所以脚本会提取全天日志,这个命令 "first_line,Last_line w filname.log" extract整个日志,但在 Vi 或 vim 编辑器中打开文件后
  • 但它会失败我们得到乘法错误..正如你所说,它只会提取以该特定日期开头的行,因此 SED 命令在这种情况下不好使用:( ...你发现我制作的脚本有任何逻辑错误...?它不工作
  • 上面的行应该替换你的 else 案例中的内容。这是你做的吗?
  • 我收到此错误 [t3718 logs]$ ./testing_script 输入应用程序名称:esqm 输入此格式的日期----'10 Jan 20:01:00':04 年 3 月 29 日:49:19 sed: -e expression #1, char 4: unterminated address regex updates: esqm.txt (stored 0%) [t3718 logs]$
  • 在这个 sed -n '/'$date'/,$ p' $app.log >$app.txt .. 如果使用 sed -n '/$date/,$ p' $ app.log >$app.txt 它会生成空文件,如果我使用你提到的代码会出错
【解决方案2】:

如果您的任务如您所描述的那样机械,您可以使用此处的文档从您的脚本向vi 传递命令。

我说的是这样的事情

#!/bin/bash
read -p " enter the App name" : app
read =p " enter the Date in this Format 10 Jan 20:01:00" : date
FILE=/logs/$app.log

# Check if $app exists or not
if test ! -f "$app" /logs
then
    echo "Error - $app not found."
else
    vi $FILE <<-_EOF_
          /date
          .=
          G
          first_line,Last_line w filname.log
          etc, etc
    _EOF_
fi

我写这篇文章只是为了让你觉得探索起来很有趣。我知道代码行不通。

【讨论】:

  • 读者注意:在结束词_EOF_之前必须有制表符,而不是空格
【解决方案3】:

我会使用 awk:

awk -v date="$date" '$0 ~ date {p=1} p' file.in > file.out

这将在第一次看到日期时开始打印行,直到行尾。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多