查找以“+”结尾的行 [英] Find lines that end with "+"

查看:183
本文介绍了查找以“+”结尾的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法形成一个grep正则表达式,它只能找到那些以+符号结尾的行。例如:

应该匹配 - 这是一个示例行+



不应该匹配 - 这是或之间的另一个+,这是另一个+,介于+

解决方案

使用 $ 来指示line:

  grep'+ $'file 



示例



  $ cat a 
这是一个示例行+
这是在
hello

$ grep'+ $'a
之间的另一个+这是一个示例行+



更新




如果我想显示行最终只有+。即使
线是这样的这是一个在bw +最后+的线。

然后您可以使用 awk
不希望匹配此行。 / code>:

  awk'/ \ + $ /&& split($ 0,a,\ +)== 2'文件



说明




  • / \ + $ / 匹配以 +

  • split($ 0,a,\ +)== 2 基于 + 分隔符的块。返回值是件数,所以 2 表示它只包含一个 +



  • 示例



      $ cat a 
    这是一个示例行+
    这是在
    之间的另一个+你好+和+
    你好

    $ awk'/ \ + $ /&& amp ; split($ 0,a,\ +)== 2'a
    这是一个示例行+


    I am unable to form a grep regex which will find me only those lines that end with + signs. Example:

    Should match - This is a sample line +

    Should not match - This is another with + in between or This is another with + in between and ends with +

    解决方案

    Do use $ to indicate the end of the line:

    grep '+$' file
    

    Example

    $ cat a
    This is a sample line +
    This is another with + in between
    hello
    
    $ grep '+$' a
    This is a sample line +
    

    Update

    What if I want to display lines which only has + in the end. Even if a line is like this This is a line with + in bw and in the end +. I don't want this line to be matched.

    Then you can use awk:

    awk '/\+$/ && split($0, a, "\+")==2' file
    

    Explanation

    • /\+$/ matches lines ending with +.
    • split($0, a, "\+")==2 divides the string in blocks based on the + delimiter. The return value is the number of pieces, so 2 means that it just contains one +.

    Example

    $ cat a
    This is a sample line +
    This is another with + in between
    Hello + and +
    hello
    
    $ awk '/\+$/ && split($0, a, "\+")==2' a
    This is a sample line +
    

    这篇关于查找以“+”结尾的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆