代码之家  ›  专栏  ›  技术社区  ›  wsdzbm

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

  •  0
  • wsdzbm  · 技术社区  · 4 年前

    我有很多xml文件,希望通过验证它们是否以 </root> 标签

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

    1 回复  |  直到 4 年前
        1
  •  1
  •   Abelisto    4 年前

    对于大型文件,如果确定目标字符串位于其末尾,请使用 tail :

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

    在约7GB的文本文件上测试,单个 grep 大约20多岁 小于0.01秒

    有关文件数(以及不包含图案的打印文件名):

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