查找以“+”结尾的行

时间:2014-03-03 12:49:42

标签: grep

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

应匹配 - This is a sample line +

不应匹配 - This is another with + in betweenThis is another with + in between and ends with +

2 个答案:

答案 0 :(得分:5)

请使用$表示该行的结尾:

grep '+$' file

实施例

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

$ grep '+$' a
This is a sample line +

更新

  

如果我想显示最后只有+的行,该怎么办?即使是   line是这样的这是一行+在bw和最后+。一世   不希望这条线匹配。

然后您可以使用awk

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

解释

  • /\+$/匹配以+结尾的行。
  • split($0, a, "\+")==2根据+分隔符在字符串中划分字符串。返回值是件数,因此2表示它只包含一个+

实施例

$ 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 +

答案 1 :(得分:1)

指定结尾+前面有任何其他+,或作为正则表达式:

grep '^[^+]*+'

fedorqui's a文件的第二版上测试时的输出:

This is a sample line +