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

问题描述 投票:2回答:2

我有很多XML文件,并希望通过验证它们是否以</root>标记结尾来检查其完整性。

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

确实很棘手,但速度很慢(文件太多又太大)。有更快的解决方案吗?

linux bash text text-processing
2个回答
1
投票

对于大文件,如果您确定目标字符串在它们的末尾,请使用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

0
投票

使用查找。

find /path/to/files -type f -name '*.xml' -exec  grep -FL '</root>' {} +

当然用正确的路径更改/path/to/files

最后的+有所作为,因为它将尽可能多地处理文件,同时避免ARG_MAX参见ARG_MAX

© www.soinside.com 2019 - 2024. All rights reserved.