我无法形成一个grep正则表达式,它只能找到那些以+符号结尾的行。例如:
应匹配 - This is a sample line +
不应匹配 - This is another with + in between
或This is another with + in between and ends with +
答案 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)