grep与正则表达式学习

grep命令:

搜索匹配打印输出

grep --- 检索、过滤文件内容
        //格式:grep [选项] 要查找的字符串或条件表达式 被查找的文件名
        -i 查找时忽略大小写
        -v 反转查找,输出与查找条件不相符的行
        -b 将可执行文件(binary)当作文本文件(text)来搜索
        -c 仅显示找到的行数
        -n 显示行号
        -A -A 2:搜索时显示匹配到的那一行以及下2行
        -B -B 2:搜索时显示匹配到的那一行以及上2行
        -C -C 2:搜索时显示匹配到的那一行以及上下2行
        -q 不输出
        -E 使用扩展的正则表达式

grep与普通正则表达式匹配:

[root@control /]# cat re_lianxi 
hello !
my name is lxb

this is my re study


today is 2022.6.6
and hello world.

①与^的精准匹配

[root@control /]# grep "^m" re_lianxi -n
2:my name is lxb

#匹配以m字符开头的句子

②与$的精准匹配

[root@control /]# grep "y$" re_lianxi -n 
4:this is my re study

#匹配最后一个字符为y的一行


#此时发现无法匹配以.或!结尾的字符行
[root@control /]# grep  "\!$"  re_lianxi -n
1:hello !
#此时为输出"!"结尾的那一行

[root@control /]# grep  "\.$"  re_lianxi -n 
8:and hello world.
#此时为输出"."结尾的那一行

③组合匹配符^$

[root@control /]# grep "^$"  re_lianxi -n
3:
5:
6:

#表示匹配文本中的空行

④"."的粗略匹配

[root@control /]# grep "."  re_lianxi -n 
1:hello !
2:my name is lxb
4:this is my re study
7:today is 2022.6.6
8:and hello world.

#"."为粗略匹配,表示匹配文本中的有内容的行

⑤转义符"\"的匹配

[root@control /]# grep "\!"  re_lianxi -n 
1:hello !
#表示匹配文本中的"!"符

[root@control /]# grep "\."  re_lianxi -n 
7:today is 2022.6.6
8:and hello world.
#匹配文本中的所以.符

⑥模糊匹配符*

[root@control /]# grep "a*"  re_lianxi -n 
1:hello !
2:my name is lxb
3:
4:this is my re study
5:
6:
7:today is 2022.6.6
8:and hello world.
#匹配文本中的出现的a字符,此时文中没有a 的行也会被匹配输出

[root@control /]# grep "c*"  re_lianxi -n 
1:hello !
2:my name is lxb
3:
4:this is my re study
5:
6:
7:today is 2022.6.6
8:and hello world.
#全文没有字符c,此时都会别匹配输出

⑦组合符.*

[root@control /]# grep ".*"  re_lianxi -n 
1:hello !
2:my name is lxb
3:
4:this is my re study
5:
6:
7:today is 2022.6.6
8:and hello world.
#表示匹配输出所有(.表示匹配所有的非空行,*表示模糊匹配所有,组合则是匹配输出所有)

⑧组合符^.$

[root@control /]# grep "^.*\."  re_lianxi -n
7:today is 2022.6.6
8:and hello world.
#贪婪匹配,表示匹配任意开头的字符,直到需要符合的字符为止

⑨中括号[]

[root@control /]# grep [2,b,t]  re_lianxi -n
2:my name is lxb
4:this is my re study
7:today is 2022.6.6
#匹配括号中存在的字符,如2,b,t(括号中的字符不一定要在文中出现)


[root@control /]# grep [^2,b,t]  re_lianxi -n
1:hello !
2:my name is lxb
4:this is my re study
7:today is 2022.6.6
8:and hello world.
#表示匹配文中除了2,b,t外的字符

扩展正则表达式:

①匹配符+

[root@control /]# grep -En "\!+" re_lianxi  
1:hello !
#表示匹配!至少一次

[root@control /]# grep -En "2+" re_lianxi  
7:today is 2022.6.6
#此时会匹配三次6

②匹配符?

[root@control /]# cat rE_lianxi 
lb
lxb
lxxb
lxxxb
lxbxbxb
lxxbxxxb
#扩展正则的匹配练习

[root@control /]# grep -En "lx?b" rE_lianxi    
1:lb
2:lxb
5:lxbxbxb
#匹配lxb中的"x"至少0或1次

③匹配符"|":

[root@control /]# grep -En "a|b" re_lianxi 
2:my name is lxb
7:today is 2022.6.6
8:and hello world.
#匹配所以包含字符a/b的行输出

④匹配符():

[root@control /]# cat RE_lianxi 
he is hi
hi si he
hi is hi
he si he


[root@control /]# grep -En "(h.).*\1" RE_lianxi  
3:hi is hi
4:he si he  
#使用括号对h*进行匹配,再通过\1与后面的h*匹配,表示匹配两个选项一样的行

[root@control /]# grep "(h.).*\1" /RE_lianxi -En 
3:hi is hi
4:he si he
[root@control /]# grep -En "^([^:]+\>).*\1$" /etc/passwd  
6:sync:x:5:0:sync:/sbin:/bin/sync
7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8:halt:x:7:0:halt:/sbin:/sbin/halt
    #表示匹配/etc/passwd下,所有开头字符和bash字符相同的行
    [^:]+ 表示匹配不以:开头的行
    ^([^:]+\>)则表示匹配非:开头的行

练习:

[root@ths /]# grep -E "^\<(root|lxb|nobody)\>" lianxin   
root:x:0:0:root:/root:/bin/bash
nobody:x:99:99:Nobody:/:/sbin/nologin
lxb:x:1000:1000:lxb:/home/lxb:/bin/bash

#找出lianxi中以root/lxb/nobody开头的行(锚点为第一行)

[root@ths /]# grep "\<[0-9]\{2,3\}\>" /etc/passwd          
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin

#找出/etc/passwd下的中2位数和3位数的行

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值