【问题标题】:shell script + match error words in the log fileshell 脚本 + 匹配日志文件中的错误词
【发布时间】:2012-02-22 15:43:21
【问题描述】:

请告知如何匹配仅出现在“]”字符之后的错误字符串 通过 awk 或 sed

   [Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found 

我的目标是统计日志文件中仅出现在“]”字符之后的所有 ERROR 单词

remark - “]”和ERROR之间必须有一个空格或多个

【问题讨论】:

    标签: shell sed awk ksh


    【解决方案1】:

    这里是一个只有 shell 的 sn-p,它应该比使用任何外部程序(用于小文件读取)更快,因为它只使用 shell 内置。可以修改它以在守护程序模式下运行时逐个处理错误(通过将日志文件拖尾到 fifo 而不是直接读取它并修改案例条件)

    不是 echo 的预期用途,但它确实将空格/制表符减少到 1 个空格

    FILE="logfile"
    ERRORS=0
    while read LINE || [ "$LINE" ]; do
        case "`echo $LINE`" in
            *\]" "ERROR*)ERRORS=$(($ERRORS+1));;
        esac
    done < "${FILE}"
    echo $ERRORS
    

    【讨论】:

    • shell while 循环是否会比grep 快,这是值得怀疑的,你有时间吗?
    • grep 一旦你找到超过 100 行的文件,就可以更快地找到那些特定的行;但是,grep 不允许您以有意义的方式操作数据,而使用 while-read-case 允许您执行一些实际有用的操作,例如对 $LINE 变量的子字符串操作或根据错误调用函数(在这种情况下,百分之几秒的差异远远超过了弥补)
    • 我同意这可能并不重要,但您确实写了“应该更快”,这是值得怀疑的。
    【解决方案2】:

    这可能对你有用:

    grep -c '\] \+ERROR' file
    

    或者

    grep -c '\][[:space:]]\+ERROR' file
    

    或者

    sed '/\]\s\+ERROR/!d' file | wc -l
    

    【讨论】:

      【解决方案3】:

      我的目标是计算所有仅出现在“]”之后的 ERROR 单词 日志文件中的字符

      remark - “]”和ERROR之间必须有一个空格或多个

      那么你就不需要像 awk、sed 甚至 perl 这样的核头了。 grep 是这样为你做的:

       grep -Pc ']\s+ERROR' yourLogFile
      

      小测试:

      kent$  echo "[Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found "|grep -Pc ']\s+ERROR'
      1
      

      【讨论】:

      • 它不适用于我的 linux - redhat 5.1 -echo "[2012 年 1 月 30 日星期一 21:14:01 IST] 错误文件 /etc/ghy.txt 未找到 "| grep -Ec ']\s+ERROR' 0
      • @Eytan 请尝试使用echo "..."|grep -Pc '...'
      【解决方案4】:

      AWK:

      awk -F"] " '/ERROR/{print $2}' inputfile
      

      测试:

      [jaypal:~] echo "[Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found " | awk -F"] " '/ERROR/{print $2}'
      ERROR file /etc/ghy.txt not found 
      

      Perl:

      perl -pe 's/.*(?<=] )(.*)/$1/' inputfile
      

      测试:

      echo "[Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found " | perl -pe 's/.*(?<=] )(.*)/$1/'
      ERROR file /etc/ghy.txt not found 
      

      计数编号。发生次数:

      [jaypal:~/Temp] cat file
      [Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found
      [Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found
      [Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found
      [Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found
      [Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found
      [Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found
      [Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found
      [Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found
      [Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found
      
      [jaypal:~/Temp] awk -F"] " '/ERROR/{a[NR]=$2}END{print "count is " length(a)}' file
      count is 9
      

      【讨论】:

        【解决方案5】:

        你可以的

        sed -n '/] ERROR/p' infile
        

        【讨论】:

        • 未能在 ERROR 之前考虑多个空格,并且使用 grep 更惯用(而且可能更快)。
        猜你喜欢
        • 2016-02-08
        • 1970-01-01
        • 2011-04-27
        • 1970-01-01
        • 2020-12-15
        • 2013-10-08
        • 1970-01-01
        • 2018-07-12
        • 1970-01-01
        相关资源
        最近更新 更多