正则表达式环境搭建:
1.linux正则表达式是处理字符串的一种方式,以行为单位;
2.linux的语系要设置为LANG=C,以免语系对正则有影响;
3.介绍正则表达式,本文使用grep,先设置alias grep="grep --color=auto"设置显示颜色;
4.示例文件如下:
[root@www ~]# cat >> test.txt<< EOF The sunset prints sorrow colour with the world Looking at the reflection in the mirror and feelings become so vacuous Escape from the darkness Where are you going toward? Staring at the fantanstic sky Stars are shining as a guide It reminds me of your smile Suddenly the rain drops from the sky Just like rose blooming in a mire Recollecting the most valuable memory Are those things only a mirage? We'll be with you and feel your mind From the moment we wake up to the last Has he become your lifeline? Though nobody can ga against time We still wish We do wish you and he could stay forever forever as one. EOF [root@www ~]#
实例:
1.查找特定字符串
[root@www ~]# grep -n "The" test.txt #-n 表示显示行号 1:The sunset prints sorrow colour with the world [root@www ~]#
2.反向查找字符/字符串
[root@www ~]# grep -vn "t" test.txt # -v 取反 3:and feelings become so vacuous 15:Has he become your lifeline? 19:forever 20:forever as one. 21: [root@www ~]#
3.忽略大小写查找
[root@www ~]# grep -in 'the' test.txt #-i 忽略大小写 1:The sunset prints sorrow colour with the world 2:Looking at the reflection in the mirror 4:Escape from the darkness 6:Staring at the fantanstic sky 9:Suddenly the rain drops from the sky 11:Recollecting the most valuable memory 14:From the moment we wake up to the last [root@www ~]#
4.查找以某个字符串开头的行 使用 ^字符串
[root@www ~]# grep '^The' test.txt -n 1:The sunset prints sorrow colour with the world [root@www ~]#
5.查找以某个字符串结尾的行 使用 字符串$
[root@www ~]# grep -n '\.$' test.txt #以.结尾,需要使用\转义 20:forever as one. [root@www ~]#
6.查找空行 使用 ^$
[root@www ~]# grep -n "^$" test.txt 21: [root@www ~]#
7.利用[]查找集合字符
[root@www ~]# grep "[cm]ome" test.txt -n #匹配come 和 mome 的行 3:and feelings become so vacuous 14:From the moment we wake up to the last 15:Has he become your lifeline? [root@www ~]# [root@www ~]# grep "[^c]ome" test.txt -n #匹配 非c外任意字符+ome 14:From the moment we wake up to the last [root@www ~]# [root@www ~]# grep '[^a-z]e' test.txt #非小写字母+e 也就是匹配A-Z+e的行 Recollecting the most valuable memory We'll be with you and feel your mind We still wish We do wish you and he could stay [root@www ~]# [root@www ~]# grep '[0-9]' test.txt -n #匹配有数字的行 3:and feelings become so vacuous 45 [root@www ~]# [root@www ~]# grep -n '^[^a-z A-Z]' test.txt #匹配开头非字符的行。 ^在[]中表示非。^在[]外表示开头(行首) 8:#It reminds me of your smile [root@www ~]#
8.特殊字符.号在正则中表示 一定有一个任意字符
[root@www ~]# grep -ni 'star...' test.txt 6:Staring at the fantanstic sky #匹配到的是Staring 7:Stars are shining as a guide #匹配到的是Stars a .必须有一个任意字符 [root@www ~]#
9.重复字符*,重复前一个字符0次或n次
[root@www ~]# grep -n 'mir*' test.txt #匹配mi mir mirr mirrrr 2:Looking at the reflection in the mirror 8:#It reminds me of your smile 10:Just like rose blooming in a mire 12:Are those things only a mirage? 13:We'll be with you and feel your mind 21:mirrrror [root@www ~]# [root@www ~]# grep -n 'g*' test.txt #匹配g0次或n次,也就是输出所有行
10.限定连续的字符范围{}
[root@www ~]# grep -n 'r\{2,6\}' test.txt #因为{}在shell有特殊意义,所以需要使用\转义。匹配r 2次到6次的行 1:The sunset prints sorrow colour with the world 2:Looking at the reflection in the mirror 21:mirrrror [root@www ~]#
11.扩展正则表达式--- + 号,匹配前一个字符1次或1次以上
[root@www ~]# grep -En 'r+o' test.txt #扩展的正则,grep需要加-E 或者使用egrep 1:The sunset prints sorrow colour with the world 2:Looking at the reflection in the mirror 4:Escape from the darkness 9:Suddenly the rain drops from the sky 10:Just like rose blooming in a mire 14:From the moment we wake up to the last 21:mirrrror [root@www ~]#
12.扩展正则表达式--- ? 号,匹配前一个字符0次或1次
[root@www ~]# egrep -n 'go?d' test.txt 22:god 25:gd [root@www ~]#
13.扩展正则表达式--- | 号,或者的意思
[root@www ~]# egrep -n 'gd|good|god' test.txt 22:god 23:good 25:gd [root@www ~]#
14.扩展正则表达式--- () 号 ,分组
[root@www ~]# egrep -n "g(oo|o)d" test.txt 22:god 23:good [root@www ~]#