zoukankan      html  css  js  c++  java
  • grep 多行 正则匹配

    https://stackoverflow.com/questions/2686147/how-to-find-patterns-across-multiple-lines-using-grep

    I relied heavily on pcregrep, but with newer grep you do not need to install pcregrep for many of its features. Just use grep -P.

    In the example of the OP's question, I think the following options work nicely, with the second best matching how I understand the question:

    grep -Pzo "abc(.|
    )*efg" /tmp/tes*
    grep -Pzl "abc(.|
    )*efg" /tmp/tes*

    I copied the text as /tmp/test1 and deleted the 'g' and saved as /tmp/test2. Here is the output showing that the first shows the matched string and the second shows only the filename (typical -o is to show match and typical -l is to show only filename). Note that the 'z' is necessary for multiline and the '(.| )' means to match either 'anything other than newline' or 'newline' - i.e. anything:

    user@host:~$ grep -Pzo "abc(.|
    )*efg" /tmp/tes*
    /tmp/test1:abc blah
    blah blah..
    blah blah..
    blah blah..
    blah efg
    user@host:~$ grep -Pzl "abc(.|
    )*efg" /tmp/tes*
    /tmp/test1

    To determine if your version is new enough, run man grep and see if something similar to this appears near the top:

       -P, --perl-regexp
              Interpret  PATTERN  as a Perl regular expression (PCRE, see
              below).  This is highly experimental and grep -P may warn of
              unimplemented features.

    That is from GNU grep 2.10.

  • 相关阅读:
    maven继承父工程统一版本号
    shiro权限控制参考
    动态查询列表页面的分页
    SVN服务器更改ip地址后怎么办
    cookie记住密码功能
    分享小插件的问题
    阿里云短信验证
    从svn上更新maven项目时,所有文件变成包的形式
    Maven工具
    Mybatis的dao层传递单参出现的问题
  • 原文地址:https://www.cnblogs.com/xuxm2007/p/9180604.html
Copyright © 2011-2022 走看看