暂无图片
暂无图片
暂无图片
暂无图片
暂无图片
尚硅谷-云计算-shell编程.pdf
143
30页
4次
2023-06-19
5墨值下载
云计算 Linux 课程系列
—————————————————————————————
第十二章:
shell
编程
尚硅谷云计算 Linux 课程
版本:V1.0
讲师:沈超
一、正则表达式
1、概述
还记得我们在上一章说过正则表达式和通配符的区别(正则表达式用来在文件中匹配符合条件
字符串,配符用来匹配符合条件的文件名)吗?其实这种区别只 Shell 当中适用,因为用来在文
件当中搜索字符串的命令,如 grepawksed 等命令可以支持正则表达式,而在系统当中搜索文件
的命令,如 lsfindcp 这些命令不支持正则表达式,所以只能使用 shell 自己的通配符来进行匹
配了。
2 基础正则表达式
元字符
* 前一个字符匹配 0 次或任意多次。
. 匹配除了换行符外任意一个字符
^ 匹配行首。例如^hello 会匹配以 hello 开头的行。
$ 匹配行尾。例如hello&会匹配 hello 结尾的行。
[]
匹配中括号中指定的任意一个字符,只匹配一个字符。
例如:[aoeiu] 匹配任意一个元音字母[0-9] 匹配任意一位数字,
[a-z][0-9]匹配小写字和一位数字构成的两位字符。
[^]
匹配除中括号的字符以外的任意一个字符。例如:[^0-9] 匹配任意
一位非数字字符[^a-z] 表示任意一位非小写字母。
\ 转义符。用于取消讲特殊符号的含义取消。
\{n\}
表示其前面的字符恰好出现 n 次。例如:[0-9]\{4\} 匹配 4
位数字,
[1][3-8][0-9]\{9\} 匹配手机号码
\{n,\}
表示其前面的字符出现不小于 n 次。例如: [0-9]\{2,\} 表示两位
及以上的数字。
\{n,m\}
表示其前面的字符至少出现 n 次,最多出现 m 次。例如:
[a-z]\{6,8\
}
匹配 6 8 的小写字母。
~/.bashrc 文件中建立这个别名:
[root@localhost ~]# vi /root/.bashrc
alias grep='grep --color=auto'
1)、 练习文件建
[root@localhost ~]# vi test_rule.txt
Mr. Li Ming said:
he was the most honest man.
123despise him.
更多云计算-Java大数据 前端 python 人工智能资料下载,可百度访问:尚硅谷官
云计算 Linux 课程系列
—————————————————————————————
But since Mr. shen Chao came,
he never saaaid those words.
5555nice!
because,actuaaaally,
Mr. Shen Chao is the most honest man
Later,Mr. Li ming soid his hot body.
2)、 “*”前一个字符匹配 0 次,或任意多次
[root@localhost ~]# grep "a*" test_rule.txt
Mr. Li Ming said:
he was the most honest man.
123despise him.
But since Mr. shen Chao came,
he never saaaid those words.
5555nice!
because,actuaaaally,
Mr. Shen Chao is the most honest man
Later,Mr. Li ming soid his hot body.
如果这样写正则表达式aa*代表这行字符串一定要有一个 a但是后面有没有 a 都可以。也就是说
会匹配至少包含有一个 a 的行:
[root@localhost ~]# grep "aa*" test_rule.txt
Mr. Li Ming s
a
id:
he w
a
s the most honest m
a
n.
But since Mr. shen Ch
a
o c
a
me,
he never s
aaa
id those words.
bec
a
use,actu
aaaa
lly,
Mr. Shen Ch
a
o is the most honest m
a
n
L
a
ter,Mr. Li ming soid his hot body
如果正则表达式是“aaa*”,则会匹配最少包含两个连续 a 的字符串,如:
[root@localhost ~]# grep "aaa*" test_rule.txt
he never s
aaa
id those words.
because,actu
aaaa
lly,
如果正则表达式是“aaaaa*”,则会匹配最少包含四个个连续 a 的字符串,如:
[root@localhost ~]# grep "aaaaa*" test_rule.txt
because,actuaaaally,
当然如果再多写一个 a,如“ aaaaaa*就不能从这篇文档中匹配任何内容了,因为我们这篇文档
a 最多的单词“actuaaaally”只有四个个连续的 a,而“aaaaaa*”会匹配最少五个连续的 a
更多云计算-Java大数据 前端 python 人工智能资料下载,可百度访问:尚硅谷官
云计算 Linux 课程系列
—————————————————————————————
3)、 “.” 匹配除了换行符外任意一个字符
正则表达式“.”只能匹配一个字符,这个字符可以是任意字符,举个例子:
[root@localhost ~]# grep "s..d" test_rule.txt
Mr. Li Ming
said
:
Later,Mr. Li ming
soid
his hot body.
#“s..d”会匹配在 s d 这两个字母之间一定有两个字符的单词
[root@localhost ~]# grep "s.*d" test_rule.txt
Mr. Li Ming
said
:
he never
saaaid
those words.
Later,Mr. Li ming
soid his hot bod
y.
#最后一句话比较有意思,匹配的是“
soid his hot bod
[root@localhost ~]# grep ".*" test_rule.txt
4) “^”匹配行首,“$”匹配行尾
“^”代表匹配行首,比如“^M”会匹配以大写“M开头的行:
[root@localhost ~]# grep "^M" test_rule.txt
M
r. Li Ming said:
M
r. Shen Chao is the most honest man
“$”代表匹配行尾,如果“n$”会匹配以小写“n结尾的行:
[root@localhost ~]# grep "n$" test_rule.txt
Mr. Shen Chao is the most honest man
而“^$”则会匹配空白行:
[root@localhost ~]# grep -n "^$" test_rule.txt
5)、 “[]” 匹配中括号中指定的任意一个字符,只匹配一个字符
“[]”会匹配中括号中指定任意一个字符,注意只能匹配一个字符。比如[ao]要不会匹配一个 a
字符,要不会匹配一个 o 字符:
[root@localhost ~]# grep "s[ao]id" test_rule.txt
而“[0-9]”会匹配任意一个数字,如:
[root@localhost ~]# grep "[0-9]" test_rule.txt
而“[A-Z]”则会匹配一个大写字母,如:
[root@localhost ~]# grep "[A-Z]" test_rule.txt
如果正则是“^[a-z]”代表匹配用小写字母开头的行:
[root@localhost ~]# grep "^[a-z]" test_rule.txt
6)“[^]匹配除中括号的字符以外的任意一个字符
[root@localhost ~]# grep "^[^a-z]" test_rule.txt
而“^[^a-zA-Z]”则会匹配不用字母开头的行:
[root@localhost ~]# grep "^[^a-zA-Z]" test_rule.txt
更多云计算-Java大数据 前端 python 人工智能资料下载,可百度访问:尚硅谷官
of 30
5墨值下载
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文档的来源(墨天轮),文档链接,文档作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。