[英]How can I find the names of log files with 'error' on the last line?
I am trying to find the id's of jobs that end in error. 我正在尝试查找以错误结尾的作业的ID。
When I use 当我使用
for i in *log
do tail -n 1 $i | grep error
echo $i
done
It seems to find error on the last line of each file, even for files that don't have errors on the last line, and returns all of the filenames with 似乎在每个文件的最后一行都找到了错误,即使对于在最后一行没有错误的文件也是如此,并使用
STOP fatal_error
out1.log
STOP fatal_error
out2.log
STOP fatal_error
out3.log
....
even though 即使
grep error out1.log
returns nothing 什么都不返回
Alternatively, is there an easier way to get a list of the jobs that end in error? 或者,是否有更简单的方法来获取错误结束的作业列表? I tagged with qsub because I use qsub to submit the jobs 我用qsub进行了标记,因为我使用qsub提交了工作
You need an if statement so that you only echo the filename when the grep succeeds: 您需要一个if语句,以便仅在grep成功时回显文件名:
for i in *.log
do
if tail -n 1 $i | grep error > /dev/null
then
echo $i
fi
done
Also, redirect the grep
results to /dev/null
so it doesn't appear in the output. 另外,将grep
结果重定向到/dev/null
,使其不会出现在输出中。
You want to say 你想说
do tail -n 1 $i | grep error
not 不
do tail -n 1 *.log | grep error
Otherwise, you are checking every log file at every iteration and will always get the same results. 否则,您将在每次迭代时检查每个日志文件,并且将始终获得相同的结果。
Your logic is incorrect. 您的逻辑不正确。 echo $i will have the listing of the file, not the grep output echo $ i将列出文件,而不是grep输出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.