shell脚本+;匹配日志文件中的错误字

shell脚本+;匹配日志文件中的错误字,shell,sed,awk,ksh,Shell,Sed,Awk,Ksh,请建议如何匹配仅在“]”字符后出现的错误字符串 通过awk或sed [Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found 我的目标是统计日志文件中仅在“]”字符之后出现的所有错误字 备注-在“]”和ERROR之间必须有一个或多个空格 sed -n '/] ERROR/p' infile AWK: 测试: Perl: 我的目标是统计仅出现在“]”之后的所有错误字 日志文件中的字符 备注-在“]”和ERROR之

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

   [Mon Jan 30 21:14:01 IST 2012] ERROR file /etc/ghy.txt not found 
我的目标是统计日志文件中仅在“]”字符之后出现的所有错误字

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

sed -n '/] ERROR/p' infile
AWK: 测试:
Perl: 我的目标是统计仅出现在“]”之后的所有错误字 日志文件中的字符

备注-在“]”和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

这里是一个纯shell代码段,它应该比使用任何外部程序(用于小文件读取)都要快,因为它只使用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
FILE=“logfile”
错误=0
读第| |[“$LINE”];做
中的大小写“`echo$LINE`”
*\]“”错误*)错误=$($ERRORS+1));;
以撒
完成<“${FILE}”
echo$错误

这可能适合您:

grep -c '\] \+ERROR' file


在出现错误之前无法解释多个空格,并且在使用
grep时更为惯用(而且可能更快)。这在我的linux-redhat 5.1-echo“[Mon Jan 30 21:14:01 IST 2012]ERROR file/etc/ghy.txt not found”| grep-Ec']\s+错误上不起作用0@Eytan请尝试使用
echo“…”|grep-Pc“…”
一个shell
虽然
循环会比
grep
快,但这是值得怀疑的,你有时间吗?grep会更快地找到那些特定的行,一旦你找到100多行的文件;但是,grep不允许您以有意义的方式操作数据,而使用while read case则允许您执行一些实际有用的操作,例如对$LINE变量进行子字符串操作,或者根据错误调用函数(在本例中,百分之二的差分比所需的差分要多)我同意这可能并不重要,但你确实写了“应该更快”,这是值得怀疑的。
[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
 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
FILE="logfile"
ERRORS=0
while read LINE || [ "$LINE" ]; do
    case "`echo $LINE`" in
        *\]" "ERROR*)ERRORS=$(($ERRORS+1));;
    esac
done < "${FILE}"
echo $ERRORS
grep -c '\] \+ERROR' file
grep -c '\][[:space:]]\+ERROR' file
sed '/\]\s\+ERROR/!d' file | wc -l