查找不以指定字符串结尾的文本文件的快速方法

fast way to find text files not ending with a specified string

我有很多 xml 文件,想通过验证它们是否以 </root> 标记结尾来检查它们的完整性。

grep -L "</root>" *.xml

做棘手但相当慢的事情(太多和大的文件)。有没有更快的解决方案?

对于大文件,如果你确定目标字符串在文件的末尾,使用tail:

tail -n 10 filename.xml | grep "</root>" # will check the last 10 lines for the pattern

测试文本文件 ~ 7GB,单个 grep ~ 20s,tail 小于 0.01s

文件数(以及不包含模式的打印文件名):

for f in *.xml ; do tail -n 10 "$f" | grep -q "</root>" || echo "$f" ; done