grep命令

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值