我无法形成一个grep正则表达式,它只会找到那些以+符号结尾的行.例:
应该匹配 - This is a sample line +
不应该匹配 - This is another with + in between
或This is another with + in between and ends with +
不要使用$
指示行的结尾:
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)