grep全面搜索正则表达式。
1.创建文件00.pem
vi 00.pem
内容
2.使用默认的grep命令
3.
查询出user字符串所在的行号(-n):
查询出user字符串有几行(-c):
创建文件01.pem
内容:
查询多个文件
多个文件时可以使用通配符
找出不包含某个字符串的所有的行(-v)
创建文件02.pem
内容
查询出beijing,不区分大小写(-i)
找出匹配hell*字符串(-w),-w表示匹配整词
4.
找出以.pem结尾的文件中以user开头的行
将grep和正则结合使用
主要参数:
-c:只输出匹配行的计数。
-I:不区分大 小写(只适用于单字符)。
-h:查询多文件时不显示文件名。
-l:查询多文件时只输出包含匹配字符的文件名。
-n:显示匹配行及 行号。
-s:不显示不存在或无匹配文本的错误信息。
-v:显示不包含匹配文本的所有行。
pattern正则表达式主要参数:
\: 忽略正则表达式中特殊字符的原有含义。
^:匹配正则表达式的开始行。
$: 匹配正则表达式的结束行。
\<:从匹配正则表达 式的行开始。
\>:到匹配正则表达式的行结束。
[ ]:单个字符,如[A]即A符合要求 。
[ - ]:范围,如[A-Z],即A、B、C一直到Z都符合要求 。
。:所有的单个字符。
* :有字符,长度可以为0。
1.创建文件00.pem
vi 00.pem
内容
1
2 hello world
3
4 user liu i am a teacher
5
6 world
7
8 world 123
9
10 hello haha
2.使用默认的grep命令
[root@iZ2546h6zurZ test]# grep hello 00.pem
hello world
hello haha
3.
查询出user字符串所在的行号(-n):
[root@iZ2546h6zurZ test]# grep -n user 00.pem
4:user liu i am a teacher
查询出user字符串有几行(-c):
[root@iZ2546h6zurZ test]# grep -c user 00.pem
1
创建文件01.pem
内容:
1 user liu
2
3 hello world
4
5 lcq welcome to beijing
6
7 haha
8
9 hello
10
11 world
查询多个文件
[root@iZ2546h6zurZ test]# grep -n hello 00.pem 01.pem
00.pem:2:hello world
00.pem:10:hello haha
01.pem:3:hello world
01.pem:9:hello
多个文件时可以使用通配符
[root@iZ2546h6zurZ test]# grep -n hello 0?.pem
00.pem:2:hello world
00.pem:10:hello haha
01.pem:3:hello world
01.pem:9:hello
找出不包含某个字符串的所有的行(-v)
[root@iZ2546h6zurZ test]# grep -n -v hello 00.pem
1:
3:
4:user liu i am a teacher
5:
6:world
7:
8:world 123
9:
11:
12:
13:
14:
15:
16:
创建文件02.pem
内容
1 hello Hello
2 world
3 World
4 Beijing
5 jinan
6 beijing
7
8
查询出beijing,不区分大小写(-i)
[root@iZ2546h6zurZ test]# grep -n beijing 02.pem
6:beijing
[root@iZ2546h6zurZ test]# grep -n -i beijing 02.pem
4:Beijing
6:beijing
找出匹配hell*字符串(-w),-w表示匹配整词
[root@iZ2546h6zurZ test]# grep -n hell* 02.pem
1:hello Hello
[root@iZ2546h6zurZ test]# grep -n -w hell* 02.pem
4.
找出以.pem结尾的文件中以user开头的行
将grep和正则结合使用
[root@iZ2546h6zurZ test]# grep ^user *.pem
00.pem:user liu i am a teacher
01.pem:user liu