简体   繁体   English

如何找到最后一行带有“错误”的日志文件的名称?

[英]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.

相关问题 如何使用“查找”在名称中包含空格的目录中搜索文件? - How can I search for files in directories that contain spaces in names, using “find”? 如何查找名称包含多个相同字符的文件? - How can I find files with names that feature several times the same character? 如何查找具有给定名称的多行文件? - How can I find files with more than one line in them with a given name? 我想在 linux 中使用 ksh 在最后找到带有额外空行的文件 - I want to find files with extra blank line in last in linux using ksh 如何在AWK命令中将文件名添加为第一行 - How can I add file names as the first line in AWK command 如何检查(和删除)最后一行(包含字符串的最后一次出现)是否有逗号作为最后一个字符? - How can I check (and delete) if the last line (that contains the last occurence of a string) has a comma as last character? 如何从目录中删除名称奇怪的文件? - How can I delete files with odd names from directories? 我怎样才能删除一个大日志文件的一行的一部分 - How can i remove a part of a line of a big log file 如何创建包含文件夹中所有文件名称的数组? - How can I creates array that contains the names of all the files in a folder? 如何记录该终端线的执行时间? - How can I log the time of the execution of this terminal line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM