嗨我当前的代码是:
find /home/user/logfilesError/ -maxdepth 1 -type f -name "gBatch_*"\
-daystart -mtime -1 -exec grep -rl "ERROR" "{}" + | xargs -l basename
if [ $? -eq 0 ]; then
tday="$(date +'%d.%m.%Y')"
echo "ERROR found on $tday in the files obove!"
else
tday="$(date +'%d.%m.%Y')"
echo "No ERROR was found at the $tday !"
fi
该代码当前输出当天(不是最近 24 小时)创建或编辑的日志文件,并搜索日志文件是否包含“错误”,并简单地说明哪个日志文件有错误,或者如果没有任何错误,他也会这么说。
我对名字进行了一些审查,所以不要认为我搞砸了,这就是它不起作用的原因;-)
输出(示例):
gBatch_2070.log
gBatch_2071.log
ERROR found on 25.06.2014 in the files obove!
该文件夹看起来像:
每个文件看起来像:
我想要的输出:
文件名+“ERROR”+错误后的消息
例子:
gBatch_2067.log - 错误 **.batch.BatchStart = Batchverarbeitung beeendet,gBatch_2077.log - 错误 **.batch.BatchStart = Batchverarbeitung beeendet,...
预先感谢您的帮助!
答案1
这应该是您搜索的内容:
find /home/user/logfilesError/ -maxdepth 1 -type f -name "gBatch_*" -daystart -mtime -1 \
-exec grep -H "ERROR" {} \; | sed -e 's/.*\/gBatch_/gBatch_/g' -e 's/:[^E]*/: /g' | tr '\n' ', '
示例输出:
gBatch_2070.log:ERROR **.batch.BatchStart = Batchverarbeitung beeendet, gBatch_2077.log - ERROR **.batch.BatchStart = Batchverarbeitung beeendet
gBatch_2070.log:ERROR **.batch.BatchStart = Batchverarbeitung beeendet, gBatch_2077.log - ERROR **.batch.BatchStart = Batchverarbeitung beeendet
gBatch_2071.log:ERROR **.batch.BatchStart = Batchverarbeitung beeendet, gBatch_2077.log - ERROR **.batch.BatchStart = Batchverarbeitung beeendet
...
解释:
-H
也强制 grep 打印文件名sed 's/.*\/gBatch_/gBatch_/g'
将文件名设为基本文件名
答案2
find /home/user/logfilesError/ -maxdepth 1 -type f -name "gBatch_*"\
-daystart -mtime -1 -exec grep -rl "ERROR" "{}" + | xargs -l basename\
> /tmp/files_found
if [ $? -eq 0 ]; then
tday="$(date +'%d.%m.%Y')"
while read line
do
error=`grep "ERROR" /home/user/logfilesError/$line`
error=`echo $error | sed 's/^.*ERROR/ERROR/' | tr '\n' ', '`
echo "$line - $error"
done < /tmp/files_found
echo "ERROR found on $tday in the files obove!"
rm /tmp/files_found
else
tday="$(date +'%d.%m.%Y')"
echo "No ERROR was found at the $tday !"
fi