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