查找以"+"结尾的行

Vik*_*hal 2 grep

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

应该匹配 - This is a sample line +

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

fed*_*qui 5

不要使用$指示行的结尾:

grep '+$' file
Run Code Online (Sandbox Code Playgroud)

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

$ grep '+$' a
This is a sample line +
Run Code Online (Sandbox Code Playgroud)

更新

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

然后你可以使用awk:

awk '/\+$/ && split($0, a, "\+")==2' file
Run Code Online (Sandbox Code Playgroud)

说明

  • /\+$/匹配以+.结尾的行.
  • 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 +
Run Code Online (Sandbox Code Playgroud)