正则表达式

1.基本正则 ^匹配行首 例:grep '^r' /etc/passwd root:x:0:0:root:/root:/bin/bash

$匹配行未 例:grep 'localhost$' /etc/hosts 127.0.0 localhost.localdomain localhost

[]集合,匹配集合中的任意单个字符 例:grep '[A-Z]' brace.txt abcab CD-ROM

[^]对集合取反 例:grep '[^ab]' brace.txt sdcgfkill

.匹配任意单个字符 例:grep '.grop' brace.txt agrop

*匹配前一个字符任意次数

例:egrep 'stuf*' /etc/rc.local want to do the full Sys V style init stuff.

{n,m}匹配前一个字符n到m次 例:grep '(ab){2,4}' brace.txt dedef abab ghighi

{n}匹配前一个字符n次 例:grep '(ab){3}' brace.txt cdcd ababab

()组合为整体,保留 {n,}匹配前一个字符n次以上 例:grep '(ab){2,}' brace.txt cdcd ababab

2.扩展正则 grep -E 支持扩展正则简写egrep

+最少匹配一次 例:egrep 'f+' /etc/rc.local want to do the full Sys V style init stuff. You can put your own initialization stuff in here if you don't

?最多匹配一次 例:egrep 'i?' /etc/rc.local want to do the full Sys V style in stuff.

{n,m}匹配n到m次 例:egrep '(ab){3}' brace.txt cdcd ababab

|或者 例:grep -E '^root|^daemon' /etc/passwd root:x:0:0:root:/root:/bin/bash daemon:x:2:2:daemon:/sbin:/sbin/nologin

()组合为整体,保留

\b单词边界 例:egrep '\binit\b' /etc/rc.local want to do the full Sys V style init stuff.

<单词的开头 例:egrep '<init>' /etc/rc.local want to do the full Sys V style init stuff.

>单词的结尾 例:egrep 'll>' /etc/rc.local want to do the full Sys V style init stuff.