以下是练习的文本文件,regular_express.txt
-----------------------------regular_express.txt----------------------------------------
"Open Source" is a good mechanism to develop programs.$
apple is my favorite food.$Football game is not use feet only.$
this dress doesn't fit me.$
However, this dress is about $ 3183 dollars.^M$
GNU is free air not free beer.^M$
Her hair is very beauty.^M$
I can't finish the test.^M$
Oh! The soup taste good.^M$
motorcycle is cheap than car.$
This window is clear.$
the symbol '*' is represented as start.$
Oh!^IMy god!$
The gd software is a library for drafting programs.^M$
You are the best is mean you are the no. 1.$
The world <Happy> is the same with "glad".$
I like dog.$
google is the best tools for search keyword.$
goooooogle yes!$
go! go! Let's go.$
# I am VBird$
$
--------------------------------------------------------------------------------------------------
例题一:搜寻特定字符串
1 搜寻'the'
#gerp -n 'the' regular_express.txt
2 反向搜寻,即不包含'the'
#grep -ni 'the' regular_express.txt
例题二:利用中括号[]来搜寻集合字符
1 搜寻test或tast,这两个单词有共同之处:‘t?tst’
#grep -n 't[ae]te' regular_express.txt
其实,[]里面不论有几个字符,都只代表[一个]字符
2寻找有oo的字符
#grep -n 'oo' regular_express.txt
3 不想让oo前面有g
#grep -n '[^g]00' regular_express.txt
4 oo前面不想有小写字母
#grep -n '[^a-z]oo' regular_express.txt
或
#grep -n '[^[:lower:]oo]' regular_express.txt
例题三:行首与行尾字符^$
1 列出the出现在行首的行
#grep -n '^the' regular_express.txt
2 要求只出现行首是小写的
#grep -n '^[a-z]' regular_express.txt
或
#grep -n '^[[:lower:]]' regular_express.txt
(注意:第一种写法成立的条件是设立的语言编码中,小写字母是连续的即a b c d,当为 a A b B ....这样的LANG=zh_TW时,第一种写法就是错误的,所以从这方面讲,我们提倡第二种写法,更多的字母表示例如[:lower:]这种,可以man grep)
3 不想让开头是英文
#grep -n '^[^a-zA-Z]' regular_express.txt
或者
#grep -n '^[^[:alpha:]]' regular_express.txt
显然^在中括号内外有不同的意义:中括号内为非,即反向选择,中括号外为匹配行首。
4 行尾为小数点
#grep -n '\.$' regular_express.txt
$为匹配行尾,而小数点,需要转义。
5 找出空行
#grep -n '^$' regular_express.txt
例题四: 任意一个字符.与重复字符*
在通配符中*表示任意个字符,但是在正规表达式中含义不同:
“.”(小数点):代表一定有一个任意字符的意思
“*”(星号): 代表重复前一个字符0到任意多次的意思
1 找出g??d的字符串,即共有四个字符,起头是g而结束是d
#grep -n 'g..d' regular_express.txt
2 找出gd,god,good等:
#grep -n 'go*d' regular_express.txt
注意*表示前面字符出现0到任意多次,因此要至少出现两个连续的o,则需要ooo*
3 找出含有数字的行
#grep -n '[0-9][0-9]*' regular_express.txt
例题五:限定连续RE 字符范围{}
1 我想找出连续两个o的字符串,这时需要使用到限定范围字符{},但因为{、}在shell中是有特殊意义的,因此需要用转义字符
# grep -n 'o\{2\}' regular_express.txt
2 我想找出两个o的字符串
#grep -n 'o\{2,5\}' regular_express.txt
3 我想找出连续两个以上o的字符串
#grep -n 'o\{2,\}' regular_express.txt