^ 行首定位符
$ 行尾定位符

# mkdir /test
# cd /test
[root@teacher test]# cp /etc/passwd  ./

# grep "root" passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

[root@teacher test]# grep "^root" passwd
root:x:0:0:root:/root:/bin/bash

[root@teacher test]# grep "bash$" passwd

root:x:0:0:root:/root:/bin/bash

# vim passwd
添加 
ro:x:0:0:root:/root:/bin/bash

[root@teacher test]# grep "^ro" passwd
root:x:0:0:root:/root:/bin/bash
ro:x:0:0:root:/root:/bin/bash
[root@teacher test]# grep "^ro:" passwd
ro:x:0:0:root:/root:/bin/bash

[root@teacher test]# grep "sh$" passwd

----------------------
.  匹配换行字符之外的任意单个字符
* 匹配前面的字符,有0个或多个

[root@teacher test]# vim s1

abc
acc
adc
qbc
wbc
ebc

[root@teacher test]# grep "a.c" s1
abc
acc
adc

[root@teacher test]# grep "..c" s1
abc
acc
adc
qbc
wbc
ebc

# grep "a*bc" s1
abc
qbc
wbc
ebc

[root@teacher test]# grep --color "a*bc" s1
abc
qbc
wbc
ebc

# grep --color ".*:" passwd
-----------------------------
[...] 匹配字符组内任意一个字符
[0-9] 0123456789
[a-z] 小写字母
[A-Z] 大写字母
[a-Z] 所有字母
[a-Z0-9] 字母或数字
[^...]取反,非字符组里的任意一个字符
[^0-9] 非数字
[^a-Z] 非字母


# grep "^[a-z][a-z][a-z]" passwd

[root@teacher test]# grep --color "^[a-z][a-z][a-z]:" passwd

# grep "x:[1-9][0-9]:" passwd --color

# grep ":[1-9][0-9]:[0-9]" passwd --color
-----------------------------
\ 屏蔽元字符的含义 meta

# vim s1

abc
acc
adc
qbc
wbc
ebc
e.c

[root@teacher test]# grep "e.c"  s1
ebc
e.c
[root@teacher test]# grep "e\.c"  s1
e.c

# cp /etc/shadow  ./
[root@teacher test]# grep '\$'  shadow 
----------------------------------
\(\) 标签,保存标签里面的字符,使用\1-9调用,最多9个标签,排列顺序,从左向右。
\(1\)...\(2\)...\(3\)  \1  \2  \3 

# grep "\(root\).*\1" passwd --color
root:x:0:0:root:/root:/bin/bash
ro:x:0:0:root:/root:/bin/bash
You have new mail in /var/spool/mail/root
[root@teacher test]# grep "\(root\).*\1.*\1" passwd --color
root:x:0:0:root:/root:/bin/bash
[root@teacher test]# grep "\(root\).*\(0\).*\2.*\1" passwd --color
root:x:0:0:root:/root:/bin/bash
You have new mail in /var/spool/mail/root

[root@teacher test]# vim a1

root,/:tom,,:root:...tom:,,,mike:,,,root,,,:mike,,:mike,,:tom,,:root

# grep "\(root\).*\(tom\).*\1.*\2.*\(mike\).*\1.*\3.*\3.*\2.*\1" a1
---------------
# vim s1

abc
acc
adc
qbc
wbc
ebc
e.c
mike and tom
把tom和mike的位置对调过来                                                             

:1,$s/\(tom\) and \(mike\)/\2 and \1/g   

# vim s1
abc
acc
adc
qbc
wbc
ebc
e.c
mike and tom
aaab
cccd
1112
3334

[root@teacher test]# grep "\(.\)\1\1" s1
aaab
cccd
1112
3334

----------------------------
x\{m\}  x(任意字符)  有m个
x\{m,\}  x(任意字符)  有>m个
x\{m,n\}  x(任意字符)  有m~n个

[root@teacher test]# grep "^[a-z][a-z][a-z]:" passwd --color

# grep "^[a-z]\{3\}:" passwd --color

[root@teacher test]# grep "^[a-z]\{3,\}:" passwd --color

# grep "^[a-z]\{3,4\}:" passwd --color

------------------------------
\< 词首定位符
\> 词尾定位符

# vim passwd
添加
roooot:x:0:0:root:/root:/bin/bash
rootooo:x:0:0:bin/bash:arooootbc

[root@teacher test]# grep "^\<root"  passwd --color

[root@teacher test]# grep "^\<ro"  passwd --color

[root@teacher test]# grep "^\<ro\>"  passwd --color
ro:x:0:0:root:/root:/bin/bash

[root@teacher test]# grep "\<root\>" passwd --color

[root@teacher test]# grep "ooot\>" passwd --color
-------------------------

POSIX  表示法
portable operating system interface
              可移植操作系统接口
[:alpha:] 字母  a-Z  [a-Z]
[:lower:] 小写字母  a-z
[:upper:] 大写字母  A-Z
[:digit:] 十进制数 0-9
[:xdigit:] 十六进制数 0-9a-F
[:alnum:] 字母+数字 a-Z0-9
[:space:] 空白字符(空格 tab)
[:punct:] 符号  
[^a-Z0-9]?
[^[:alpha:]]  取反

# vim s1
mike and tom
aaab
cccd
1112
3334
   abcdefg                    //使用space键加空格
                        hello   //使用tab键加空格

# grep '^[[:space:]]'  s1
# grep '[[:punct:]]'  s1 --color

-------------------------------
扩展正则

egrep = grep -E
支持的基本正则元字符   ^$.*[]\ \< \>

|        或

# egrep  'NW|WE' datafile 
northwest       NW      Charles Main            3.0     .98       3       34
western         WE      Sharon Gray             5.3     .97       5       23
western         WE      Sharon Gray             5.3     .97       5       23

# grep -E 'NW|WE' datafile
northwest       NW      Charles Main            3.0     .98       3       34
western         WE      Sharon Gray             5.3     .97       5       23
western         WE      Sharon Gray             5.3     .97       5       23

?  前面的条件匹配0次或1次

[root@teacher test]# egrep 'a?bc' s1
abc
qbc
wbc
ebc
   abcdefg

---------------------
+   前面的条件匹配一次或多次
# vim s1
abcabcabc
[root@teacher test]# egrep 'a+bc' s1
abc
   abcdefg

[root@teacher test]# egrep '(abc)+' s1 --color

[root@teacher test]# egrep 'aa+' s1
aaab

# egrep '^[a-z]+:' passwd --color

------------------------
()   标签或一组条件

# egrep '(root).*\1.*\1' passwd --color
root:x:0:0:root:/root:/bin/bash

# vim s2
at
atat
atatat
aataataat

# egrep '(at){3}' s2 --color
atatat

-----------------------
x{m}  x{m,}  x{m,n}   条件重复多少次

# egrep '(at){2,3}' s2 --color
atat
atatat
-----------------------
fgrep

[root@teacher test]# fgrep 'e.c' s1

[root@teacher test]# fgrep '$' s1
,%$?+-)(

-----------------------------
grep
-i   不去分大小写
-v   取反
-c   匹配的行数
-n   显示行号
-o   只显示匹配内容
--color  色彩显示匹配的内容
-r   递归查找

# cd /test
# grep -r 'root' /test

-l    //当于r选项结合使用时把包含正则表达式的文件名列出
# grep -rl  'root'  /test
/test/passwd
/test/loginfile
/test/a1
/test/shadow

-A num  显示匹配内容下num行
-B num  显示匹配内容上num行
-num    显示匹配内容上下num行

# vim s3
1
2
3
4  abc
5
6
7
8
9

[root@teacher test]# grep -A 2 'abc'  s3
4  abc
5
6
[root@teacher test]# grep -B 2 'abc'  s3
2
3
4  abc
You have new mail in /var/spool/mail/root
[root@teacher test]# grep -2 'abc'  s3
2
3
4  abc
5
6

# cat -n passwd
     1  root:x:0:0:root:/root:/bin/bash
     2  roooot:x:0:0:root:/root:/bin/bash
     3  rootooo:x:0:0:/bin/bash:arooootbc
     4  ro:x:0:0:root:/root:/bin/bash
     5  bin:x:1:1:bin:/bin:/sbin/nologin
     6  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     7  adm:x:3:4:adm:/var/adm:/sbin/nologin
     8  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     9  sync:x:5:0:sync:/sbin:/bin/sync
    10  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    11  halt:x:7:0:halt:/sbin:/sbin/halt
    12  mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    13  news:x:9:13:news:/etc/news:
    14  uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
    15  operator:x:11:0:operator:/root:/sbin/nologin
    16  games:x:12:100:games:/usr/games:/sbin/nologin
    17  gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
    18  ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    19  nobody:x:99:99:Nobody:/:/sbin/nologin
    20  nscd:x:28:28:NSCD Daemon:/:/sbin/nologin


[root@teacher test]# cat -n passwd|egrep '[[:space:]]+1'   
     1  root:x:0:0:root:/root:/bin/bash
    10  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    11  halt:x:7:0:halt:/sbin:/sbin/halt
    12  mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    13  news:x:9:13:news:/etc/news:
    14  uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
    15  operator:x:11:0:operator:/root:/sbin/nologin
    16  games:x:12:100:games:/usr/games:/sbin/nologin
    17  gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
    18  ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    19  nobody:x:99:99:Nobody:/:/sbin/nologin