linux中的正则表达式-位置匹配

^:表示锚定行首,此字符后面的任意内容必须出现在行首,才能匹配。
例如:

[root@iZbp19obnr01zl0jrho17wZ data]# cat reget 
hello world
hi     hello

hello ,zyc
[root@iZbp19obnr01zl0jrho17wZ data]# grep "^hello" reget 
hello world
hello ,zyc

$:表示锚定行尾,此字符前面的任意内容必须出现在行尾,才能匹配。
例如:

[root@iZbp19obnr01zl0jrho17wZ data]# grep "hello$" reget 
hi     hello

^$:表示匹配空行,这里所描述的空行表示”回车”,而”空格”或”tab”等都不能算作此处所描述的空行。

^abc$:表示abc独占一行时,会被匹配到。
例如:

[root@iZbp19obnr01zl0jrho17wZ data]# cat reget 
hello world
hi     hello

hello
hello ,zyc
[root@iZbp19obnr01zl0jrho17wZ data]# grep "^hello$" reget 
hello

<或者\b :匹配单词边界,表示锚定词首,其后面的字符必须作为单词首部出现。
例如:

[root@iZbp19obnr01zl0jrho17wZ data]# cat REG 
abchello world
abc helloabc abc
abc abchelloabc abc
[root@iZbp19obnr01zl0jrho17wZ data]# grep "\

>或者\b :匹配单词边界,表示锚定词尾,其前面的字符必须作为单词尾部出现。
例如:

[root@iZbp19obnr01zl0jrho17wZ data]# cat REG 
abchello world
abc helloabc abc
abc abchelloabc abc
[root@iZbp19obnr01zl0jrho17wZ data]# grep "hello\b" REG 
abchello world
[root@iZbp19obnr01zl0jrho17wZ data]# grep "hello\>" REG 
abchello world

\B:匹配非单词边界,与\b正好相反。

你可能感兴趣的:(Linux三剑客,linux,grep)