7.1使用句點匹配單字符
句點“.”可以匹配任意單字符
“.”允許匹配A S C I I集中任意字符,或爲字母,或爲數字
7.2 在行首以^匹配字符串或字符序列
$ ls -l| grep "^d"
drwxrwxrwx 2 hzmct hzmct 4096 9月 1 21:19 2
$ ls -l| grep "d..x..x..x"
drwxrwxrwx 2 hzmct hzmct 4096 9月 1 21:19 2
7.3 在行尾以$匹配字符串或字符
可以說$與^正相反,它在行尾匹配字符串或字符, $符號放在匹配單詞後。
如果要匹配所有空行,執行以下操作:
^$
具體分析爲匹配行首,又匹配行尾,中間沒有任何模式,因此爲空行。
如果只返回包含一個字符的行,操作如下:
^.$
不像空白行,在行首與行尾之間有一個模式,代表任意單字符。
//-n 顯示行號
//查找只包含一個字符的行
$ grep -n "^.$" 2.txt
11:u
//查找空行
$ grep -n "^$" 2.txt
10:
$ grep -n "trouble$" 2.txt
4:hello trouble
6:hello is trouble
7.4 使用*匹配字符串中的單字符或其重複序列
使用此特殊字符匹配任意字符或字符串的重複多次表達式
hzmct@U-64:/study/linuxtest/day03$ grep -n "compu*ter" 2.txt
13:computer
14:compuuter
15:compuuuuter
16:compuuuuuutering
7.5 使用\屏蔽一個特殊字符的含義
$ grep -n "\." 2.txt
10:.1.2.3
12:*.pass
$ grep -n "\^" 2.txt
11:^note
$ grep -n "\*\.pass" 2.txt
12:*.pass
7.6 使用[]匹配一個範圍或集合
使用[ ]匹配特定字符串或字符串集,可以用逗號將括弧內要匹配的不同字符串分開,但並
不強制要求這樣做(一些系統提倡在複雜的表達式中使用逗號),這樣做可以增加模式的可讀
性。
使用“-”表示一個字符串範圍,表明字符串範圍從“ -”左邊字符開始,到“-”右邊字
符結束。
如果熟知一個字符串匹配操作,應經常使用 [ ]模式。
//匹配包含任意一個數字的行
$ grep -n [1-9] 2.txt
10:.1.2.3
19:123456789
23:ASdf123
hzmct@U-64:/study/linuxtest/day03$ grep -n "[1-9]" 2.txt
1:48
2:48p
3:48
5:ll48o
7:48f
10:.1.2.3
19:123456789
23:ASdf123
//匹配包含任意一個小寫字母的行
$ grep -n [a-z] 2.txt
2:48p
4:hello trouble
5:ll48o
6:hello is trouble
7:48f
8:qq
9:hostnames
11:^note
12:*.pass
13:u
14:HostNAMES
15:computer
16:compuuter
17:compuuuuter
18:compuuuuuutering
20:asdfghl
23:ASdf123
24:sjHlt
//匹配包含任意一個字母的行,不區分大小寫
hzmct@U-64:/study/linuxtest/day03$ grep -n [A-Za-z] 2.txt
2:48p
4:hello trouble
5:ll48o
6:hello is trouble
7:48f
8:qq
9:hostnames
11:^note
12:*.pass
13:u
14:HostNAMES
15:computer
16:compuuter
17:compuuuuter
18:compuuuuuutering
20:asdfghl
21:ASDFGKL
22:ASDFGKL
23:ASdf123
24:sjHlt
//匹配包含以s開頭p結尾,且中間是任意一個字母的行
$ grep -n "s[A-Z a-z]p" 2.txt
26:sop
//匹配包含以s開頭p結尾,且中間是任意一個字母或者數字的行
$ grep -n "s[A-Z a-z 0-9]p" 2.txt
26:sop
27:s2p
//匹配Computer或者computer兩個單詞
$ grep -n "[Cc]omputer" 2.txt
15:computer
16:Computer
//匹配以字母o或u開頭,後跟任意一個字符任意次,並以 t結尾的任意字
hzmct@U-64:/study/linuxtest/day03$ grep -n "[ou].*t" 2.txt
4:hello trouble
6:hello is trouble
9:hostnames
11:^note
14:HostNAMES
15:computer
16:Computer
17:compuuter
18:compuuuuter
19:compuuuuuutering
26:sot
30:scout
31:shout
32:bought
//匹配包含system後跟句點的所有單詞
$ grep -n "[Ss]ystem\." 2.txt
25:system.1
26:System.2
[ ]在指定模式匹配的範圍或限制方面很有用。結合使用 *與[ ]更是有益,例如 [A-Za-z]*將
匹配所有單詞
$ grep -n "[A-Za-z]*" 2.txt
1:48
2:48p
3:48
4:hello trouble
5:ll48o
6:hello is trouble
7:48f
8:qq
9:hostnames
10:.1.2.3
11:^note
12:*.pass
13:u
14:HostNAMES
15:computer
16:Computer
17:compuuter
18:compuuuuter
19:compuuuuuutering
20:123456789
21:asdfghl
22:ASDFGKL
23:ASDFGKL
24:ASdf123
25:system.1
26:System.2
27:sjHlt
28:sot
29:sop
30:s2p
31:sjHltp
32:scout
33:shout
34:bought
注意^符號的使用,當直接用在第一個括號裏,意指否定或不匹配括號裏內容。
[^a-zA-Z] 匹配任一非字母型字符,而
[^0-9] 匹配任一非數字型字符
hzmct@U-64:/study/linuxtest/day03$ grep -n [^A-Za-z] 2.txt
1:.1.2.3
2:hello 2 trouble
18:123456789
hzmct@U-64:/study/linuxtest/day03$ grep -n [^0-9] 2.txt
1:.1.2.3
2:hello 2 trouble
3:ll48o
4:hello is trouble
5:48f
6:qq
7:hostnames
7.7使用\{\}匹配模式結果出現的次數
使用*可匹配所有匹配結果任意次,但如果只要指定次數,就應使用 \{\},此模式有三種
形式,即:
pattern\{n\} 匹配模式出現n次。
pattern\{n,\} 匹配模式出現最少n次。
pattern\{n,m} 匹配模式出現n到m次之間, n , m爲0~255中任意整數
//查找u出現1次的行
$ grep -n 'u\{1\}' 2.txt
2:hello 2 trouble
4:hello is trouble
11:u
13:computer
14:Computer
15:compuuter
16:compuuuuter
17:compuuuuuutering
29:scout
30:shout
31:bought
//查找u出現2次的行
$ grep -n 'u\{2\}' 2.txt
15:compuuter
16:compuuuuter
17:compuuuuuutering
//查找u至少出現2次的行
$ grep -n 'u\{2,\}' 2.txt
15:compuuter
16:compuuuuter
17:compuuuuuutering
//查找u出現2~4次的行
hzmct@U-64:/study/linuxtest/day03$ grep -n 'u\{2,4\}' 2.txt
15:compuuter
16:compuuuuter
17:compuuuuuutering
查找u出現2~4次的行,且以B結尾
$ grep -n 'u\{2,4\}B' 2.txt
16:compuuuuB
//查找匹配數字出現4次,後跟代碼XX,最後是數字出現4次
$ grep -n '[0-9]\{4\}XX[0-9]\{4\}' 2.txt
34:4523XX9001