匹配参数,匹配文件/目录名字 : *.txt *.sh lidao{1,4}.txt
* | 所有 |
---|---|
{} | 生成序列 |
[] | 【a-z】匹配小写字母,一个中括号相当于一个字符 |
[^] | 取反排除 |
? | 任何一个字符 |
匹配所有 *.log *.txt
# 找出当前目录下.log 及.avi 文件
ls *.log
find ./ -name '*.avi'
# 找出系统中包含catalina的文件
find / -type f -name "*catalina*"
生成序列-数字与字母
#01
[root@localhost ~]# echo {01..10}
01 02 03 04 05 06 07 08 09 10
[root@localhost ~]# echo {A..Z}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
#02
[root@localhost ~]# echo {01..10}YANG
01YANG 02YANG 03YANG 04YANG 05YANG 06YANG 07YANG 08YANG 09YANG 10YANG
[root@localhost ~]# echo YANG{01..10}
YANG01 YANG02 YANG03 YANG04 YANG05 YANG06 YANG07 YANG08 YANG09 YANG10
#03
[root@localhost ~]# echo {1,5,9}
1 5 9
[root@localhost ~]# echo A{,B}
A AB
#生成有规律的序列(了解)
[root@localhost ~]# seq 1 2 10
1
3
5
7
9
[root@localhost ~]# echo {1..10..2}
1 3 5 7 9
[root@localhost ~]# echo {a..z..2}
a c e g i k m o q s u w y
#01
[root@localhost ~]# ls /bin/?
/bin/[ /bin/w
[root@localhost ~]# ls /bin/??
/bin/ar /bin/cd /bin/dd /bin/fc /bin/ln /bin/nl /bin/ps /bin/rz /bin/su /bin/ul
/bin/as /bin/ci /bin/df /bin/fg /bin/ls /bin/nm /bin/rb /bin/sb /bin/sx /bin/vi
/bin/bg /bin/co /bin/du /bin/id /bin/m4 /bin/od /bin/rm /bin/sg /bin/sz /bin/wc
/bin/cc /bin/cp /bin/ex /bin/ld /bin/mv /bin/pr /bin/rx /bin/sh /bin/tr /bin/xz
[root@localhost ~]# ls /bin/???
/bin/a2p /bin/cat /bin/dwp /bin/g++ /bin/idn /bin/ocs /bin/rev /bin/seq /bin/tbl /bin/vim
/bin/awk /bin/cmp /bin/dwz /bin/gcc /bin/ldd /bin/pic /bin/rpm /bin/ssh /bin/tee /bin/who
/bin/c++ /bin/col /bin/env /bin/gdb /bin/lex /bin/ptx /bin/rvi /bin/sum /bin/tic /bin/xxd
/bin/c89 /bin/cpp /bin/eqn /bin/gio /bin/lua /bin/pwd /bin/s2p /bin/svn /bin/toe /bin/yes
/bin/c99 /bin/cut /bin/f95 /bin/git /bin/lz4 /bin/raw /bin/scp /bin/tac /bin/top /bin/yum
/bin/cal /bin/dir /bin/fmt /bin/gpg /bin/man /bin/rcs /bin/sed /bin/tar /bin/tty /bin/zip
引号系列 | |
---|---|
单引号 | 所见即所得,单引号里的内容会被原封不动的输出(大部分命令) |
双引号 | 与单引号类似,双引号的里面的特殊符号会被解析 |
不加引号 | 与双引号类似,支持通配符 |
反引号 | 优先执行命令 |
[root@localhost ~]# echo 'Yang-zs $LANG `hostname` $(whoami) {1..5}'
Yang-zs $LANG `hostname` $(whoami) {1..5}
[root@localhost ~]# echo "Yang-zs $LANG `hostname` $(whoami) {1..5}"
Yang-zs zh_CN.UTF-8 localhost.localdomain root {1..5}
[root@localhost ~]# echo Yang-zs $LANG `hostname` $(whoami) {1..5}
Yang-zs zh_CN.UTF-8 localhost.localdomain root 1 2 3 4 5
主要用来进行匹配字符(三剑客过滤文件内容)
匹配字符:手机号,身份证号码
通过正则表达式匹配内容
1️⃣所有符号都是英文
2️⃣刚开始学习的时候,推荐使用grep显示,正则执行的过程
3️⃣注意系统的语言与字符集(了解)
基础正则 BRE
扩展正则 ERE
正则分类 |
---|
基础正则 ^ $ ^$ . * .* [] [^] |
扩展正则 | + {} () ? |
区别 | 处理目标 | 支持的命令不同 |
---|---|---|
通配符 | 文件/目录 文件名 处理的是参数 | Linux大部分命令都支持 |
正则 | 进行过滤,在一个文件中查找内容,处理的是字符 | Linux三剑客,开发语言:Python,GoLang |
准备环境
[root@localhost ibjs]# cat re.txt
I an ShuaiGe
I like Linux
my blog is http://127.0.0.1
My qq is 28728222
not 882812311
my god,i am not Shuaige,but OLDBOY!
BRE-基础正则
1️⃣^ 以……开头的行
#01 ^ 以……开头的行
[root@localhost ibjs]# grep '^my' re.txt
my blog is http://127.0.0.1
my god,i am not Shuaige,but OLDBOY!
[root@localhost ibjs]# grep '^M' re.txt
My qq is 28728222
2️⃣$ 以……结尾的行
# $ 以……结尾的行
[root@localhost ibjs]# grep 'x$' re.txt
I like Linux
[root@localhost ibjs]# grep '1$' re.txt
my blog is http://127.0.0.1
not 882812311
3️⃣ ^$ 匹配空行
# ^$ 匹配空行
空行非空格,如果有空格不算空行
[root@localhost ibjs]# grep -n '^$' re.txt
3:
4:
8:
9:
# 排除文件的空行和注释行
root@localhost ibjs]# grep -n -v '^$' /etc/ssh/sshd_config | grep -v '#'
22:HostKey /etc/ssh/ssh_host_rsa_key
24:HostKey /etc/ssh/ssh_host_ecdsa_key
25:HostKey /etc/ssh/ssh_host_ed25519_key
32:SyslogFacility AUTHPRIV
47:AuthorizedKeysFile .ssh/authorized_keys
65:PasswordAuthentication yes
69:ChallengeResponseAuthentication no
79:GSSAPIAuthentication yes
80:GSSAPICleanupCredentials no
96:UsePAM yes
101:X11Forwarding yes
126:AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
127:AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
128:AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
129:AcceptEnv XMODIFIERS
132:Subsystem sftp /usr/libexec/openssh/sftp-server
4️⃣ . (点)表示任意一个字符
# 以任意字符开头的行 会自动去除空行
[root@localhost ibjs]# grep -n '^.' re.txt
1:I an ShuaiGe
2:I like Linux
5:my blog is http://127.0.0.1
6:My qq is 28728222
7:not 882812311
10:my god,i am not Shuaige,but OLDBOY!
# 练习:过滤出文件中以 . 结尾的行(运用转义符,将 . 转义为字符即可匹配到)
[root@localhost ibjs]# grep -n '.$' re.txt
1:I an ShuaiGe.
2:I like Linux.
9:yes.
转义字符 | 含义 |
---|---|
/ | 转义,去掉特殊含义 |
/n | 换行符 |
/t | 制表符 |
# /n
[root@localhost ibjs]# echo -e 'lllnllll'
lll
llll
# t
[root@localhost ibjs]# echo -e 'llllltllllll'
lllll llllll
5️⃣ * 前一个字符连续出现0次或者0次以上
#连续出现
#连续出现0次=没有
0 #连续出现一次
000 #连续出现三次
0000 #连续出现多次
#01 文件连续出现的0
[root@localhost ibjs]# grep '0*' re.txt
I an ShuaiGe.
I like Linux.
my blog is http://127.0.0.1
My qq is 28728222
not 882812311
yes.
my god,i am not Shuaige,but OLDBOY!
## 正则表达式表示连续或者所有的时候,会出现尽可能多的匹配,称为贪婪性
6️⃣ .* 表示所有
#01 以所有开头到o的内容
[root@localhost ibjs]# grep '^.*o' re.txt
my blog is http://127.0.0.1
not 882812311
my god,i am not Shuaige,but OLDBOY!
#02 以I开头并且以.结尾的行
[root@localhost ibjs]# grep '^I.*.$' re.txt
I am ShuaiGe.
I like Linux.
7️⃣ [] 表示匹配字符 a&b&c 中任意一个,中括号表示一整体
#01
[root@localhost ibjs]# grep '[abc]' re.txt
I an ShuaiGe.
my blog is http://127.0.0.1
my god,i am not Shuaige,but OLDBOY!
#02 匹配数字及字母精简写法
[root@localhost ibjs]# grep '[0-9]' re.txt
my blog is http://127.0.0.1
My qq is 28728222
not 882812311
[root@localhost ibjs]# grep '[a-z]' re.txt
I an ShuaiGe.
I like Linux.
my blog is http://127.0.0.1
My qq is 28728222
not 882812311
yes.
my god,i am not Shuaige,but OLDBOY!
#03 中括号中想匹配什么就写什么,多余的不用
[root@localhost ibjs]# grep -i '[a-zA-Z]' re.txt
I an ShuaiGe.
I like Linux.
my blog is http://127.0.0.1
My qq is 28728222
not 882812311
yes.
my god,i am not Shuaige,but OLDBOY!
#练习:
#01 匹配文件中大写字母开头的行
[root@localhost ibjs]# grep '^[A-Z]' re.txt
I an ShuaiGe.
I like Linux.
My qq is 28728222
My xxxxxxxx xx xxxxx
Ix 1233333
Bi jjjxjd
#02 匹配文件中以大写字母开头并且以小写字母或者空格结尾的行
[root@localhost ibjs]# grep '^[A-Z].*[a-z ]$' re.txt
My xxxxxxxx xx xxxxx
Ix 1233333
Bi jjjxjd
8️⃣ [^] 排除:[^abc] 匹配不是a,不是b,不是c的内容
#01
[root@localhost ibjs]# grep '[^abc]' re.txt
I an ShuaiGe.
I like Linux.
my blog is http://127.0.0.1
My qq is 28728222
not 882812311
yes.
my god,i am not Shuaige,but OLDBOY!
My xxxxxxxx xx xxxxx
Ix 1233333
Bi jjjxjd
基础正则-BRE小结
BRE | 含义 |
---|---|
^ | 以……开头的行 |
$ | 以……结尾的行 |
^$ | 空行,没有任何符号的行 |
. | 任何一个字符 |
* | 前一个连续出现0次或者出现0次以上 |
.* | 表示 所有 ,贪婪性:表现 连续出现及所有的时候 |
[] | [abc],[a-z],[0-9],[a-z0-9A-Z] [abc] 每次匹配括号内的一个字符,或者的意思,a&b&c |
[^] | [^abc] 排除:匹配不是a,不是b,不是c的内容 |
1️⃣ + 前一个字符出现1次或者1次以上
在如今egrep已经不推荐使用,推荐使用 grep -E 来支持扩展正则
#01 过滤出连续出现的e
[root@localhost ibjs]# grep -E 'e+' re.txt
I an ShuaiGe.
I like Linux.
yes.
#02 过滤出连续出现的数字
[root@localhost ibjs]# grep -E '[0-9]+' re.txt
my blog is http://127.0.0.1
My qq is 28728222
not 882812311
Ix 1233333
#03 过滤出连续出现的单词MY
[root@localhost ibjs]# grep -E -i 'MY+' re.txt
my blog is http://127.0.0.1
My qq is 28728222
my god,i am not Shuaige,but OLDBOY!
My xxxxxxxx xx xxxxx
2️⃣ | 表示或者
# 匹配 I 或者 MY
[root@localhost ibjs]# grep -E 'I|my' re.txt
I an ShuaiGe.
I like Linux.
my blog is http://127.0.0.1
my god,i am not Shuaige,but OLDBOY!
Ix 1233333
# | 与 [] 的区别
#共同点:都可以表示或者
#区别:
#| 表示的或者可以是单词也可以是字符 a|b|c ,am|my
#[] 表示的或者是 某个字符 [abc]
3️⃣ {} a{n,m} 前一个字符a连续出现至少n次,最多出现m次
{}格式 | |
---|---|
a{n,m} | 前一个字符至少出现n次,最多出现m次 |
a{n} | 前一次字符连续出现n次 |
a{n,} | 前一个字符至少出现n次 |
a{,m} | 前一次字符最多出现m次 |
#01 匹配数字0,出现了至少1次最多3次的数字0
[root@localhost ibjs]# grep -E '0{1,3}' re.txt
my blog is http://127.0.0.1
0
000
000000000
000000
0000
#02 匹配连续出现3次的数字0
[root@localhost ibjs]# grep -E '0{3}' re.txt
000
000000000
000000
0000
3️⃣ () 被括起来的内容相当于是一个整体;sed命令的后向引用(反向引用)
#
[root@localhost ibjs]# grep -E '0(.|0)0' re.txt
my blog is http://127.0.0.1
000
000000000
000000
0000
4️⃣ ? 表示前一个字符出现0次或者1次
# 筛选 gd或者god
[root@localhost ibjs]# grep -E 'gd|god' re.txt
my god,i am not Shuaige,but OLDBOY!
god
gd
[root@localhost ibjs]# grep -E 'go?d' re.txt
my god,i am not Shuaige,but OLDBOY!
god
gd
6️⃣扩展正则-ERE小结
ERE | 含义 |
---|---|
+ | 前一个字符出现一次或者多次 |
| | 或者 |
{} | a{n,m}前一个字符出现至少n次,最多m次 |
() | 1.表示整体 2.sed后向引用(反向引用)分组 |
? | 表示前一个字符出现0次或者1次 |
7️⃣ 中括号表达式(了解)
# [[:alnum:]]
#表示 数字 0-9 字母 a-z A-Z
[root@localhost ibjs]# grep '[[:alnum:]]' re.txt
I an ShuaiGe.
I like Linux.
my blog is http://127.0.0.1
My qq is 28728222
not 882812311
yes.
my god,i am not Shuaige,but OLDBOY!
My xxxxxxxx xx xxxxx
Ix 1233333
Bi jjjxjd
0
000
000000000
000000
0000
432511120011123341
god
df
gd
8️⃣ grep命令选项
grep选项 | |
---|---|
-n | 显示行号 |
-v | 取反 |
-i | 不区分大小写 |
-o | 显示grep执行过程 |
-E | 支持扩展正则,也识别基础正则 |
-w | 精确过滤 |
-R | 递归查找 |
-l(小写的L) | 只显示文件名 |
-A | grep过滤的时候显示内容及下面的一行 |
-B | grep过滤的时候显示内容及上面的一行 |
-C | grep过滤的时候显示内容及上下的一行 |
# grep 精确过滤
[root@localhost ibjs]# grep -w '0' re.txt
my blog is http://127.0.0.1
0
# grep 找出/etc/目录下包含oldboy的文件
#方法1 通用
grep 'oldboy' `find /etc -type f `
#方法2 适用比较简单的
grep -R 'oldboy' /etc
#找出系统某个目录中是否包含病毒链接 www.bingdu.com
grep -Rl 'www.bingdu.com' /etc
连续重现(重复出现) | |
---|---|
+ | 1次或多次 |
{} | 任意次范围 |
* | 0次及多次 |
? | 0次或者1次 |
整体 | |
---|---|
[] | [abc],a或b或c |
() | 表示整体 |
[^] | 表示取反 |
| | 或者 |
其他 | |
---|---|
^ | 表示以……开头 |
$ | 表示以……结尾 |
^$ | 表示空行 |
.* | 表示所有 |
. | 表示任意一个字符 |
参与评论
手机查看
返回顶部