我有很多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
使用查找。
find /path/to/files -type f -name '*.xml' -exec grep -FL '</root>' {} +
当然用正确的路径更改/path/to/files
。
最后的+
有所作为,因为它将尽可能多地处理文件,同时避免ARG_MAX
参见ARG_MAX