cat >re.txt<<EOF
I am a teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is https://www.cnblogs.com/daofaziran
our size is http://blog..com
my qq is 12345600000
not 65400321000.
my god ,i am not gril,but god!
EOF
2.3.2 使用
点击查看代码
'''
grep -o 显示正则匹配的内容
正则表达式有贪婪性: 表示连续出现或所有的时候,尽可能多匹配
grep -v vs [^]
-v 以行为单位.
[^] 字符
转义字符,脱掉马甲打回原形.去掉符号特殊含义
'''# ^my开头行
[root@Kylin-V10-sp3 ~/test]# grep ^my re.txt
my blog is https://www.cnblogs.com/daofaziran
my qq is 12345600000
my god ,i am not gril,but god!
[root@Kylin-V10-sp3 ~/test]# # 以com 结尾的行
[root@Kylin-V10-sp3 ~/test]# grep 'com $' re.txt
our size is http://blog..com
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]# # ^$ 空行,这行中没有任何字符,包含空格 查看配置文件的时候排除空行.
[root@Kylin-V10-sp3 ~/test]# grep -nv '^$' re.txt
1:I am a teacher!
2:I teach linux.
4:I like badminton ball ,billiard ball and chinese chess!
5:my blog is https://www.cnblogs.com/daofaziran
6:our size is http://blog..com
8:my qq is 12345600000
9:not 65400321000.
11:my god ,i am not gril,but god!
[root@Kylin-V10-sp3 ~/test]# # 任意一个字符刚开始学正则,较少单独使用.
[root@Kylin-V10-sp3 ~/test]# grep -n '.' re.txt
1:I am a teacher!
2:I teach linux.
4:I like badminton ball ,billiard ball and chinese chess!
5:my blog is https://www.cnblogs.com/daofaziran
6:our size is http://blog..com
8:my qq is 12345600000
9:not 65400321000.
11:my god ,i am not gril,but god!
[root@Kylin-V10-sp3 ~/test]# # 连续出现,重复 a* 前一个字符连续出现0次或0次以上 与*一起使用
[root@Kylin-V10-sp3 ~/test]# grep '0*' re.txt
I am a teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is https://www.cnblogs.com/daofaziran
our size is http://blog..com
my qq is 12345600000
not 65400321000.
my god ,i am not gril,but god!
#.* 所有
[root@Kylin-V10-sp3 ~/test]# grep '^.*http' re.txt
my blog is https://www.cnblogs.com/daofaziran
our size is http://blog..com
# 以my开头的行以空格结尾的行
[root@Kylin-V10-sp3 ~/test]# grep '^my.* $' re.txt
my blog is https://www.cnblogs.com/daofaziran
[root@Kylin-V10-sp3 ~/test]# # [] [abc] 一个整体,每次匹配1个字符,匹配a或b或c# 常用格式
[root@Kylin-V10-sp3 ~/test]# grep '[a-z]' re.txt
I am a teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is https://www.cnblogs.com/daofaziran
our size is http://blog..com
my qq is 12345600000
not 65400321000.
my god ,i am not gril,but god!
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]# grep '[A-Z]' re.txt
I am a teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]# grep '[0-9]' re.txt
my qq is 12345600000
not 65400321000.
[root@Kylin-V10-sp3 ~/test]# # 大小写
[root@Kylin-V10-sp3 ~/test]# grep '[a-z,A-Z]' re.txt
I am a teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is https://www.cnblogs.com/daofaziran
our size is http://blog..com
my qq is 12345600000
not 65400321000.
my god ,i am not gril,but god!
[root@Kylin-V10-sp3 ~/test]# grep '[a-zA-Z]' re.txt
I am a teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is https://www.cnblogs.com/daofaziran
our size is http://blog..com
my qq is 12345600000
not 65400321000.
my god ,i am not gril,but god!
[root@Kylin-V10-sp3 ~/test]# grep '[a-Z]' re.txt
I am a teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is https://www.cnblogs.com/daofaziran
our size is http://blog..com
my qq is 12345600000
not 65400321000.
my god ,i am not gril,but god!
[root@Kylin-V10-sp3 ~/test]## 大小写加上数字
[root@Kylin-V10-sp3 ~/test]# grep '[a-zA-Z0-9]' re.txt
I am a teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is https://www.cnblogs.com/daofaziran
our size is http://blog..com
my qq is 12345600000
not 65400321000.
my god ,i am not gril,but god!
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]# grep '[0-Z]' re.txt
I am a teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is https://www.cnblogs.com/daofaziran
our size is http://blog..com
my qq is 12345600000
not 65400321000.
my god ,i am not gril,but god!
[root@Kylin-V10-sp3 ~/test]# # 过滤出re.txt文件中以m或n开头并且以空格 点叹号结尾的行
[root@Kylin-V10-sp3 ~/test]# grep '^[mn].*[ .!$]' re.txt
my blog is https://www.cnblogs.com/daofaziran
my qq is 12345600000
not 65400321000.
my god ,i am not gril,but god!
[root@Kylin-V10-sp3 ~/test]## [^] [^abc] 一个整体,每次匹配1个字符,非a或非b或非c
[root@Kylin-V10-sp3 ~/test]# grep '[^abc]' re.txt
I am a teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is https://www.cnblogs.com/daofaziran
our size is http://blog..com
my qq is 12345600000
not 65400321000.
my god ,i am not gril,but god!
[root@Kylin-V10-sp3 ~/test]## 过滤出文件中以.结尾的行
[root@Kylin-V10-sp3 ~/test]# grep '\.$' re.txt
I teach linux.
not 65400321000.
# 单独字符使用
[root@Kylin-V10-sp3 ~/test]# egrep '0+' re.txt
my qq is12345600000not65400321000.# +与[]一起使用
[root@Kylin-V10-sp3 ~/test]# egrep '[0-9]+' re.txt
my qq is12345600000not65400321000.
[root@Kylin-V10-sp3 ~/test]# egrep -o '[0-9]+' re.txt 1234560000065400321000# 统计文件中的单词出现次数.
[root@Kylin-V10-sp3 ~/test]# egrep -o '[a-z]+' re.txt | sort | uniq -c1 a
2 am
1and1 badminton
2 ball
1 billiard
2 blog
1 but
1 chess
1 chinese
1 cnblogs
2 com
1 daofaziran
2 god
1 gril
1 http
1 https
1 i
3is1 like
1 linux
3 my
2not1 our
1 qq
1 size
1 teach
1 teacher
1 www
[root@Kylin-V10-sp3 ~/test]## 统计文件中的字符出现的次数
[root@Kylin-V10-sp3 ~/test]# egrep -o '.' re.txt | sort | uniq -c333 !
3 ,
6 .
5 /
2 :
10021222324252613 a
8 b
7 c
6 d
8 e
1 f
6 g
6 h
13 i
3 I
1 k
12 l
8 m
9 n
12 o
2 p
2 q
5 r
9 s
10 t
3 u
3 w
1 x
3 y
2 z
[root@Kylin-V10-sp3 ~/test]# egrep -o '[^ ]' re.txt | sort | uniq -c |sort -rn13 i
13 a
12 o
12 l
10 t
1009 s
9 n
8 m
8 e
8 b
7 c
6 h
6 g
6 d
6 .
5 r
5 /
3 y
3 w
3 u
3 I
3 ,
3 !
2 z
2 q
2 p
2625242322212 :
1 x
1 k
1 f
2.4.3 {} 连续出现,重复 a{n,m} 前一个字符a,连续出现至少n次,最多m次.
'''
a{n,m}
a{m}
'''# 表示范围
[root@Kylin-V10-sp3 ~/test]# egrep '0{1,3}' re.txt
my qq is 12345600000
not 65400321000.
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]# egrep -o '0{1,3}' re.txt
000
00
00
000
[root@Kylin-V10-sp3 ~/test]# # 精确的匹配
[root@Kylin-V10-sp3 ~/test]# egrep -o '0{3}' re.txt
000
000
[root@Kylin-V10-sp3 ~/test]# egrep '0{3}' re.txt
my qq is 12345600000
not 65400321000.
[root@Kylin-V10-sp3 ~/test]#
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix