day013-Linux基础-正则表达式

5次阅读
没有评论

2026年3月24

知识点回顾

1.tar打包压缩
语法结构:tar [参数] 包名 压缩文件
#打包1.txt
tar -zcvf 1.tar.gz 1.txt
#打包etc/hosts
tar -zcf h.tar.gz /etc/hosts
#打包多个文件
tar -zcf all.tar.gz 1.txt /etc/hosts
#排除2.txt进行打包
tar -zcf a.tar.gz --exclude=2.txt *.txt
#排查ex.txt里的名字进行打包
tar -zcf ab.tar.gz --exclude-from=ex.txt *.txt
#查看包里的压缩了哪些文件
tar -tf all.tar.gz
#解压1.tar.gz
tar -xf 1.tar.gz
#解压到/opt目录
tar -xf 1.tar.gz -C /opt/
2.zip打包压缩
语法结构: zip 包名 打包文件
#打包1.txt
zip 1.zip 1.txt
#打包/etc/hosts
zip h.zip /etc/hosts
#解压
unzip 1.zip
#解压到opt
unzip 1.zip -d /opt/
3.包命名
#用主机名命名
tar -zcvf `hostname`.tar.gz 1.txt
#用系统时间命名
tar -zcvf `date +%F`.tar.gz 1.txt
4.特殊符号
; 不管前面成功还是失败,后面都一定执行!
echo a;mkdir all;cd mkdir;touch 1.txt;
echo a;mkdir all;cd all;touch 1.txt;
&& 前面成功,后面才执行
echo a&&mkdir all&&cd all&&touch 1.txt
echo a&&mkdir all&&c all&&touch 1.txt
|| 前面失败,后面才执行
echo a||mkdir all||cd all||touch 1.txt
ech a||mkdi all||c all||touch 1.txt
5.^ 指定行首内容
grep '^a' 1.txt
6.$ 指定行尾内容查找
grep 'a$' 1.txt

01.正则表达式-普通正则

环境准备:
vim oldboy.txt
I am lizhenya teacher!
I teach linux.
test

I like badminton ball ,billiard ball and chinese chess!
my blog is http: blog.51cto.com 
our site is http:www.lizhenya.com 
my qq num is 593528156

aaaa,
not 572891888887.
^^^^^^^^66$$$$$$$^^^$$
lizhenyalizhenyalizhenya
1.符号:^
作用:找什么字符开头的
[root@oldboy ~]# grep '^I' oldboy.txt 
I am lizhenya teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
2.符号:$
作用:找出以什么字符结尾的行
[root@oldboy ~]# grep 't$' oldboy.txt 
vim oldboy.txt
test
grep 语法:    grep 'xxx' file
                car 1.txt|grep 'xx'
                df -h|grep 'xx'
案例1:找出df -h磁盘信息中以/结尾的行
[root@oldboy ~]# df -h|grep '/$'
/dev/sda3        48G   14G   35G  29% /
案例2:过滤出包含sda3的行
[root@oldboy ~]# df -h|grep 'sda3'
/dev/sda3        48G   14G   35G  29% /
案例3:找出以/dev/sda开头的行
[root@oldboy ~]# df -h|grep '^/dev/sda'
/dev/sda3        48G   14G   35G  29% /
/dev/sda1       195M  139M   56M  72% /boot
案例4:找出以/dev/sda3开头的行
[root@oldboy ~]# df -h|grep '^/dev/sda3'
/dev/sda3        48G   14G   35G  29% /
案例5:找出文件中的#开头的行、并且进行取反
[root@oldboy ~]# cat /etc/selinux/config|grep -nv '#'
1:
8:SELINUX=enforcing
15:SELINUXTYPE=targeted
16:
18:SETLOCALDEFS=0
19:
20:
案例6:找出以上命令结果中的空行进行取反
[root@oldboy ~]# cat /etc/selinux/config |grep -v '#'|grep -nv '^$'
2:SELINUX=enforcing
3:SELINUXTYPE=targeted
5:SETLOCALDEFS=0
案例7:单双引号问题
#注意
''单引号 所见即所得,不能解析变量
"" 双引号 和不加引号 可以解析变量
[root@oldboy ~]# m=test
[root@oldboy ~]# echo $m
test
[root@oldboy ~]# grep '$m' oldboy.txt ;echo $?
1
[root@oldboy ~]# grep "$m" oldboy.txt ;echo $?
test
0
3.符号:.
作用:表示任意单个字符
案例1:过滤任意单个字符-o查看匹配过程
[root@oldboy ~]# grep '.' oldboy.txt -o
v
i
m
案例2:查找以.结尾的行
[root@oldboy ~]# grep '\.$' oldboy.txt 
I teach linux.
not 572891888887.
案例3:统计passwd中每个字母出现的次数从多到少进行排序
[root@oldboy ~]# grep '.' /etc/passwd -o|sort|uniq -c|sort -rn|head|grep -v :
    155 n
    135 /
    127 o
    109 s
    102 i
     78 e
     68 r
     63 b
     61 t
案例4:tr命令
作用:主要用来替换特殊符号 一对一的替换
将文件中的:或者/或者数字替换成空格
#案例:统计passwd中单词的数量
tr "需要替换的内容" "替换成啥"
语法1
[root@oldboy ~]# cat /etc/passwd|tr ":/0-9" " "|xargs -n1
语法2 使用输入重定向符号
[root@oldboy ~]# tr ":/0-9" " " < /etc/passwd |xargs -n1
4.符号:*
通配符:用来找文件的
ll *
rm -rf *
find / -name "*.txt"

正则表达式:用来过滤文件内容的、表示前面的字符出现0次或者0次以上的
如果字符没有、则默认显示所有行
grep '1*' file
[root@oldboy ~]# grep '8' oldboy.txt  -o
8
8
8
8
8
8
8
[root@oldboy ~]# grep '8*' oldboy.txt -o
8
8
88888
案例1:过滤任意字符出现0次或0次以上的(了解)
[root@oldboy ~]# grep '.*' oldboy.txt 
vim oldboy.txt
I am lizhenya teacher!
I teach linux.
test

I like badminton ball ,billiard ball and chinese chess!
my blog is http: blog.51cto.com 
our site is http:www.lizhenya.com 
my qq num is 593528156

aaaa,
not 572891888887.
^^^^^^^^66$$$$$$$^^^$$
lizhenyalizhenyalizhenya
案例2:了解下.*的贪婪匹配
[root@oldboy ~]# grep '^.*m' oldboy.txt 
vim oldboy.txt
I am lizhenya teacher!
I like badminton ball ,billiard ball and chinese chess!
my blog is http: blog.51cto.com 
our site is http:www.lizhenya.com 
my qq num is 593528156
5.符号:[]
作用:表示任意或者的含义、中括号中表示的是一个值
[abc]
案例1:过滤文件中必须abc的行
grep 'abc' file
[root@oldboy ~]# grep 'abc' oldboy.txt 
案例2:过滤文件中包含a或者b或者c的行
[root@oldboy ~]# grep '[abc]' oldboy.txt 
vim oldboy.txt
I am lizhenya teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is http: blog.51cto.com 
our site is http:www.lizhenya.com 
aaaa,
lizhenyalizhenyalizhenya
案例3.过滤文件中包含.$^的行
[root@oldboy ~]# grep '[.$^]' oldboy.txt 
vim oldboy.txt
I teach linux.
my blog is http: blog.51cto.com 
our site is http:www.lizhenya.com 
not 572891888887.
^^^^^^^^66$$$$$$$^^^$$
案例4.[^]在中括号最前面的^表示对里面的内容进行取反(了解)
[root@oldboy ~]# grep '[^abc]' oldboy.txt 
vim oldboy.txt
I am lizhenya teacher!
I teach linux.
test
I like badminton ball ,billiard ball and chinese chess!
my blog is http: blog.51cto.com 
our site is http:www.lizhenya.com 
my qq num is 593528156
aaaa,
not 572891888887.
^^^^^^^^66$$$$$$$^^^$$
lizhenyalizhenyalizhenya
#和-v的区别:用除了abc的其他字符进行搜索匹配而非结果的不展示,-v则是结果不包括匹配行
案例5:[^^]后面的^就是^
[root@oldboy ~]# grep '[^^]' oldboy.txt 
vim oldboy.txt
I am lizhenya teacher!
I teach linux.
test
I like badminton ball ,billiard ball and chinese chess!
my blog is http: blog.51cto.com 
our site is http:www.lizhenya.com 
my qq num is 593528156
aaaa,
not 572891888887.
^^^^^^^^66$$$$$$$^^^$$
lizhenyalizhenyalizhenya
案例6:过滤文件中包含所有数字的行
[root@oldboy ~]# grep '[0-9]' oldboy.txt 
my blog is http: blog.51cto.com 
my qq num is 593528156
not 572891888887.
^^^^^^^^66$$$$$$$^^^$$
案例7:过滤文件中包含所有a-z的行 -i不区分大小写
[root@oldboy ~]# grep -i '[a-z]' oldboy.txt 
vim oldboy.txt
I am lizhenya teacher!
I teach linux.
test
I like badminton ball ,billiard ball and chinese chess!
my blog is http: blog.51cto.com 
our site is http:www.lizhenya.com 
my qq num is 593528156
aaaa,
not 572891888887.
lizhenyalizhenyalizhenya
---------------------------------
[root@oldboy ~]# grep -i '[a-z0-9.,:^$]' oldboy.txt 
vim oldboy.txt
I am lizhenya teacher!
I teach linux.
test
I like badminton ball ,billiard ball and chinese chess!
my blog is http: blog.51cto.com 
our site is http:www.lizhenya.com 
my qq num is 593528156
aaaa,
not 572891888887.
^^^^^^^^66$$$$$$$^^^$$
lizhenyalizhenyalizhenya
---------------理解中括号的任意单个含义
[root@oldboy ~]# ll
总用量 4
-rw-r--r-- 1 root root   0  3月 23 15:38 12.txt
-rw-r--r-- 1 root root   0  3月 23 15:38 1.txt
-rw-r--r-- 1 root root 279  3月 23 14:17 oldboy.txt
[root@oldboy ~]# ll [0-9].txt
-rw-r--r-- 1 root root 0  3月 23 15:38 1.txt
[root@oldboy ~]# ll [0-9][0-9].txt
-rw-r--r-- 1 root root 0  3月 23 15:38 12.txt
[root@oldboy ~]# ll [10-20].txt
-rw-r--r-- 1 root root 0  3月 23 15:38 1.txt
[root@oldboy ~]# touch 5.txt
[root@oldboy ~]# ll [10-20].txt
-rw-r--r-- 1 root root 0  3月 23 15:38 1.txt
-------------------------------------------
[root@oldboy ~]# grep '[a-Z]' oldboy.txt 
vim oldboy.txt
I am lizhenya teacher!
I teach linux.
test
I like badminton ball ,billiard ball and chinese chess!
my blog is http: blog.51cto.com 
our site is http:www.lizhenya.com 
my qq num is 593528156
aaaa,
not 572891888887.
lizhenyalizhenyalizhenya
--------------------------
在中括号中没有特殊含义(除了开头^符号)
[root@oldboy ~]# grep '[!$]' oldboy.txt 
I am lizhenya teacher!
I like badminton ball ,billiard ball and chinese chess!
^^^^^^^^66$$$$$$$^^^$$

02.正则表达式-扩展正则

1.符号:+
作用:过滤前一个字符出现1次及1次以上
使用扩展正则:
语法1 grep -E
[root@oldboy ~]# grep -E '8+' oldboy.txt 
my qq num is 593528156
not 572891888887.
语法2 egrep
[root@oldboy ~]# egrep '8+' oldboy.txt
my qq num is 593528156
not 572891888887.

显示匹配过程
[root@oldboy ~]# egrep '8+' oldboy.txt -o
8
8
88888
2.符号:|
作用:或者的含义
案例1:找出文件中包含test或者Linux的行
[root@oldboy ~]# grep -E 'test|linux' oldboy.txt 
I teach linux.
test
案例2:找出空行和#进行取反
[root@oldboy ~]# egrep -v '^$|#' oldboy.txt 
vim oldboy.txt
I am lizhenya teacher!
I teach linux.
test
I like badminton ball ,billiard ball and chinese chess!
my blog is http: blog.51cto.com 
our site is http:www.lizhenya.com 
my qq num is 593528156
aaaa,
not 572891888887.
^^^^^^^^66$$$$$$$^^^$$
lizhenyalizhenyalizhenya
-------------------------------
[root@oldboy ~]# egrep '^$|#' /etc/selinux/config -v
SELINUX=enforcing
SELINUXTYPE=targeted
SETLOCALDEFS=0
3.符号:{}
作用:
{n} #前面的字符最多n次
{n,m} #至少n次,最多m次,匹配的时候先匹配最多
案例1: 8出现最多3次的行
[root@oldboy ~]# grep -E '8{3}' oldboy.txt 
not 572891888887.
案例2:取出任意连续的3次的数字
[root@oldboy ~]# grep -E '[0-9]{3}' oldboy.txt 
my qq num is 593528156
not 572891888887.
#匹配显示过程
[root@oldboy ~]# grep -E '[0-9]{3}' oldboy.txt -o
593
528
156
572
891
888
887
案例3:取出正确的身份证号
[root@oldboy ~]# cat id.txt 
李 2113421234
张 500224197
王 1233423423432oldboy
万 5oldboy
吕 lzy235872451234814
孔 150000123874591242
夏 222113859123487192
赵 37142518322922103X
-------------------------
[root@oldboy ~]# egrep '[0-9]{18}' id.txt 
孔 150000123874591242
夏 222113859123487192
#或者使用
[root@oldboy ~]# egrep '[0-9]{17}[0-9X]' id.txt 
孔 150000123874591242
夏 222113859123487192
赵 37142518322922103X
#边界符\b[]\b
[root@oldboy ~]# egrep '\b[0-9]{17}[0-9X]\b' id.txt 
孔 150000123874591242
夏 222113859123487192
赵 37142518322922103X
案例4:过滤8出现的最少2次最多3次
[root@oldboy ~]# egrep '8{2,3}' oldboy.txt 
not 572891888887.
[root@oldboy ~]# egrep '8{2,3}' oldboy.txt -o
888
88
4.符号:()
作用:将字符串作为一个整体
[root@oldboy ~]# egrep 'lizhenya+' oldboy.txt 
I am lizhenya teacher!
our site is http:www.lizhenya.com 
lizhenyalizhenyalizhenya
---------------------
[root@oldboy ~]# egrep 'lizhenya+' oldboy.txt -o
lizhenya
lizhenya
lizhenya
lizhenya
lizhenya
-----------------------
[root@oldboy ~]# egrep '(lizhenya)+' oldboy.txt -o
lizhenya
lizhenya
lizhenyalizhenyalizhenya
案例:一个整体
[root@oldboy ~]# egrep 'lizhea|nya' oldboy.txt 
I am lizheaya teacher!
our site is http:www.lizhenya.com 
lizhenyalizhenyalizhenya
-----------------------------------------------
[root@oldboy ~]# egrep 'lizhe(a|n)ya' oldboy.txt 
I am lizheaya teacher!
our site is http:www.lizhenya.com 
lizhenyalizhenyalizhenya
[root@oldboy ~]# egrep 'lizhe(a|n)ya' oldboy.txt -o
lizheaya
lizhenya
lizhenya
lizhenya
lizhenya
[root@oldboy ~]# egrep 'lizhea|nya' oldboy.txt -o
lizhea
nya
nya
nya
nya
-------------------------------------------
sed中会用()
表达式总结
过滤文件内容、支持命令 grep sed awk
1.^  #以什么开头的行   *****
2.$  #以什么结尾的行   *****
3..  #任意单个字符    不常用
4.*  #出现0次或者0次以上 不常用
5.[] #表示任意单个或者 ***** [^a] #反向匹配
6.+  #连续1次及以上   *****
7.|  #或者的含义     *****
8.{} #{n,m}前面的字符出现至少n次之多m次  {n} 最多n次 **
9.() #作为一个整体 **
学习方法: 从无法连接虚拟机那天到这周的 每个同学都要录制表达截止下周一。
1.多敲反复练习
2.输出(效率最高)9点半-10点互相问题-->录视频表达
  • [ ] 补录视频第一周
  • [ ] 补录视频第二周
  • [ ] 本周视频录制
正文完
 0
评论(没有评论)