查找以“+”结尾的行 [英] Find lines that end with "+"
问题描述
我无法形成一个grep正则表达式,它只能找到那些以+符号结尾的行。例如:
应该匹配 - 这是一个示例行+
不应该匹配 - 使用 如果我想显示行最终只有+。即使 然后您可以使用 I am unable to form a grep regex which will find me only those lines that end with + signs. Example: Should match - Should not match - Do use
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
这篇关于查找以“+”结尾的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!这是或
之间的另一个+,这是另一个+,介于+
$
来指示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
这是一个示例行+
This is a sample line +
This is another with + in between
or This is another with + in between and ends with +
$
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
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 +