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

grep-v在迁移后不再排除模式

  •  0
  • jerrygarciuh  · 技术社区  · 5 年前

    我们的一个共享托管站点最近被移动了。新服务器是Red Hat 4.8.5-36。其他二进制文件的版本是grep(GNU grep)2.20和find(GNU findutils)4.5.11

    find /home/example/example.com/public_html/ -mmin -12 \
        | grep -v 'error_log|logs|cache'
    

    搬家后 -v 似乎是无效的,我们得到的结果是

    /home/example/example.com/public_html/products/cache/ssu/pc/d/5/c
    

    结果的变化发生在移动之后。有人知道为什么它现在坏了吗?另外,如何恢复过滤后的输出?

    0 回复  |  直到 5 年前
        1
  •  1
  •   Jotne    5 年前

    如果你想排除一组词。

    grep -v -e 'error_log' -e 'logs' -e 'cache' file
    

    awk 您可以:

    awk '!/error_log|logs|cache/' file
    

    它将排除包含这些单词的所有行。

        2
  •  0
  •   Benjamin W.    5 年前
    grep -v 'error_log|logs|cache'
    

    只排除包含字面意思的字符串 error_log|logs|cache

    grep -Ev 'error_log|logs|cache'
    

    gnugrep支持alternation作为基本正则表达式的扩展,但是 | 需要逃逸,因此这可能也可以:

    grep -v 'error_log\|logs\|cache'
    

    但是,grep不是首先需要的,我们可以使用(GNU) find 做所有的工作:

    find /home/example/example.com/public_html/ -mmin -12 \
        -not \( -name '*error_log*' -or -name '*logs*' -or -name '*cache*' \)
    

    find /home/example/example.com/public_html/ -mmin -12 \
        \! \( -name '*error_log*' -o -name '*logs*' -o -name '*cache*' \)
    

    或者,如果你 找到 -regex (GNU和BSD 找到

    find /home/example/example.com/public_html/ -mmin -12 \
        -not -regex '.*\(error_log\|logs\|cache\).*'