通配符
作用:
用来查找文件名称(不是查看过滤文件内容)
通配符进行泛查找文件名
Linux的命令都支持通配符
Shell的内置功能
环境准备:
[root@ahui ~]#mkdir /ahui/
[root@ahui ~]#cd /ahui/
[root@ahui /ahui]#touch stu{00…10}.txt
[root@ahui /ahui]#touch ahui{00…10}.log
[root@ahui /ahui]#touch ahui.txt stu.txt
第一个通配符: *
作用: 表示任意字符串 表示所有
案例1:查找当前所有文件的详细信息
[root@ahui /ahui]#ll *
-rw-r--r-- 1 root root 0 May 13 14:43 ahui00.log
-rw-r--r-- 1 root root 0 May 13 14:43 ahui01.log
2)查找以stu开头的文件
[root@ahui /ahui]#find ./ -type f -name "stu*"
[root@ahui /ahui]#find ./ -type f -name "*.txt"
案例2:使用ll查看以stu开头的文件
[root@ahui /ahui]#ll stu*
-rw-r--r-- 1 root root 0 May 13 14:42 stu00.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu01.txt
案例3:找出以.txt结尾的文件
[root@ahui /ahui]#ll *.txt
-rw-r--r-- 1 root root 0 May 13 14:43 ahui.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu00.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu01.txt
案例4:查找以stu开头并且以.txt结尾的文件
[root@ahui /ahui]#ll stu*.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu00.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu01.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu02.txt
案例5查找名称中包含root的文件
[root@ahui /ahui]#touch test-ahui
[root@ahui /ahui]#touch 1-ahui.txt
[root@ahui /ahui]#touch ahui01.log
[root@ahui /ahui]#touch test-ahui
[root@ahui /ahui]#touch 1-ahui.txt
[root@ahui /ahui]#touch ahui01.log
[root@ahui /ahui]#ll *ahui*
第二个通配符: ?
作用: 表示任意单个字符
案例1查找以stu00.开头,以任意三个字符结尾的文件
[root@ahui /ahui]#ll stu00.???
-rw-r--r-- 1 root root 0 May 13 14:42 stu00.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu01.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu02.txt
案例2查找以任意五个字符开头,以.txt结尾的文件
[root@ahui /ahui]#ll ?????.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu00.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu01.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu02.txt
第三个通配符: []
作用: 表示或者的含义 可选择
[abc]
假如我们使用ll [abc].txt
含义: 查找出当前目录是否包含 a.txt b.txt c.txt 而不是abc.txt
注意: 使用序列[0-9] 找出文件中 0123456789任意单个数字的
0-9已经所有的数字 1-10是错误的写法
[root@ahui /ahui]#ll stu0[0-9].txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu00.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu01.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu02.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu03.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu04.txt
案例1: 查找目录下所有包含stu1 stu2 stu3的文件
[root@ahui /ahui]#ll stu0[123].txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu01.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu02.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu03.txt
案例2: 使用序列的方式查找文件名称 [1-5]
不能写成[1…5]
[root@ahui /ahui]#ll stu0[1-5].txt #ll stu0[1-5].txt ======= 相等于 stu0[12345]
-rw-r--r-- 1 root root 0 May 13 14:42 stu01.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu02.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu03.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu04.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu05.txt
混合匹配
[root@ahui /ahui]#ll st??[0123].txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu00.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu01.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu02.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu03.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu10.txt
案例3: 查找文件名称以s或者o开头的
[root@ahui /ahui]#ll s*
-rw-r--r-- 1 root root 0 May 13 14:42 stu00.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu01.txt
[root@ahui /ahui]#ll o*
-rw-r--r-- 1 root root 0 May 13 14:43 ahui00.log
-rw-r--r-- 1 root root 0 May 13 14:51 ahui01.log
ll [so] # 查找文件名称是 s 或者 o的
[root@ahui /ahui]#ll [os]*
-rw-r--r-- 1 root root 0 May 13 14:43 ahui00.log
-rw-r--r-- 1 root root 0 May 13 14:51 ahui01.log
-rw-r--r-- 1 root root 0 May 13 14:43 ahui02.log
-rw-r--r-- 1 root root 0 May 13 14:43 ahui.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu00.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu01.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu02.txt
第四个通配符: {}
作用: 生成序列 数字 字母
和[]区别
{}是序列 产生序列 {a…z}{a…z}
[]或者的含义 [0-9][0-9]
[root@ahui /ahui]#echo stu{01..10}.txt
stu01.txt stu02.txt stu03.txt stu04.txt stu05.txt stu06.txt stu07.txt stu08.txt stu09.txt stu10.txt
[root@ahui /ahui]#ll stu{01..10}.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu01.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu02.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu03.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu04.txt
[root@ahui /ahui]#ll stu0{1..10}.txt
ls: cannot access stu010.txt: No such file or directory
-rw-r--r-- 1 root root 0 May 13 14:42 stu01.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu02.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu03.txt
比较蠢的写法
[root@ahui /ahui]#ll [a-z][a-z][a-z][0-9][0-9].txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu00.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu01.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu02.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu03.txt
{}和[]区别
[root@ahui /ahui]#ll stu{01..10}.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu01.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu02.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu03.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu04.txt
[root@ahui /ahui]#
[root@ahui /ahui]#ll stu[0-9][0-9].txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu00.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu01.txt
-rw-r--r-- 1 root root 0 May 13 14:42 stu02.txt
[!]取反[!abc]———、意思相同不同写法的取反
[^] 取反[^abc]_____/
[root@ahui /ahui]#ll [!os]*
-rw-r--r-- 1 root root 0 May 13 14:51 1-ahui.txt
-rw-r--r-- 1 root root 0 May 13 14:51 test-ahui
[root@ahui /ahui]#ll [^os]*
-rw-r--r-- 1 root root 0 May 13 14:51 1-ahui.txt
-rw-r--r-- 1 root root 0 May 13 14:51 test-ahui
正则表达式:
作用:
通配符是用来查找文件的
正则是用来过滤文件内容
正则分类:
基础正则表达式 BRE basic regular expression
扩展正则表示 ERE extended regular expression
注意: 中英文
“” ‘’ ·· 【】 ,。
“” ‘’ `` [] {} , .
grep 别名 在执行grep时候 系统自动加了参数 --color
在centos7.x系统自动加上 在centos6.x默认没有这个参数
centos6.x系统 过滤内容显示颜色:
vim /etc/profile
alias grep='grep --color'
语法结构:
grep ‘’ file
准备文件
[root@lzy ]# cat ahui.txt
I am ahui!
I teach linux.
test
$0 aaaa
$# bbbbbb
I like badminton ball ,billiard ball and chinese chess!
my blog is http: blog.51cto.com
our site is http:www.mhtnmsl.com
my qq num is 1001
aaaa,
not 0001.
^^^^^^^^66$$$$$$$^^^$$
ahuiahuiahui
第一个基础正则: ^
作用: 以什么开头 将以什么内容开头的行过滤出来
案例1: 查找文件中以m开头的行
[root@ahui ~]#grep '^m' ahui.txt
my blog is http: blog.51cto.com
my qq num is 1001
第二个基础正则: $
作用: 以什么结尾
案例1: 过滤以m 结尾的行
[root@ahui ~]#grep 'm $' ahui.txt
my blog is http: blog.51cto.com
our site is http:www.mhtnmsl.com
案例2: 过滤所有的空格 鼠标选中然后查看空格有颜色
[root@ahui ~]#grep ’ ’ ahui.txt
I am ahui!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is http: blog.51cto.com
our site is http:www.mhtnmsl.com
my qq num is 1001
not 0001.
案例3: 过滤文件中的空行
^$ 表示空行
[root@ahui ~]#grep '^$' ahui.txt
案例4: 显示行号
[root@ahui ~]#grep -n 'ahui' ahui.txt
1:I am ahui!
10:our site is http:www.mhtnmsl.com
16:ahuiahuiahui
第三个正则: .
作用: 匹配任意单个字符 不匹配空行 匹配空格
案例1: 匹配单个任意字母
[root@ahui ~]#grep . ahui.txt
I am ahui!
$ahui
I teach linux.
test
$0 aaaa
$# bbbbbb
-o 显示匹配执行过程
[root@ahui ~]#grep . ahui.txt -o
案例2: 查找以任意字符结尾的行
[root@ahui ~]#grep '.$' ahui.txt
I am ahui!
$ahui
I teach linux.
test
$0 aaaa
$# bbbbbb
案例3: 查找以.结尾的行
[root@ahui ~]#grep '\.$' ahui.txt
I teach linux.
not 0001.
第四个基础正则: *
作用: 前一个字符连续出现了0次或0次以上
案例1:
[root@ahui ~]#grep '8*' ahui.txt -o
8
8
88888
案例2: .* 表示所有 类似通配符的*
[root@ahui ~]#grep '.*' ahui.txt -o
I am ahui!
$ahui
I teach linux.
test
$0 aaaa
$# bbbbbb
案例3: 贪婪匹配 有多少匹配多少
查找任意字符+m开头的行(注意.* 贪婪匹配)
[root@ahui ~]#grep '^.*m' ahui.txt
I am ahui!
I like badminton ball ,billiard ball and chinese chess!
my blog is http: blog.51cto.com
our site is http:www.mhtnmsl.com
my qq num is 1001
第五个基础正则: []
作用: 或者的含义
案例1: 查找包含 a或b或c的行
[root@ahui ~]#grep '[abc]' ahui.txt
I am ahui!
$ahui
I teach linux.
$0 aaaa
$# bbbbbb
案例2: 通过序列的方式查找包含内容的行
[root@ahui ~]#grep '[a-z]' ahui.txt
I am ahui!
$ahui
I teach linux.
test
过滤不区分大小写 -i参数
[root@ahui ~]#grep -i '[a-z]' ahui.txt
I am ahui!
$ahui
I teach linux.
test
$0 aaaa
$# bbbbbb
案例3: 匹配任意单个数字0123456789的行
[root@ahui ~]#grep '[0123456789]' ahui.txt -o
5
1
1
0
.... #太长
[root@ahui ~]#grep '[0-9]' ahui.txt
$0 aaaa
my blog is http: blog.51cto.com
my qq num is 1001
not 0001.
^^^^^^^^66$$$$$$$^^^$$
案例4: 区分大小写匹配 同时匹配大写和小写的任意字符串
[root@ahui ~]#grep '[a-zA-Z]' ahui.txt
I am ahui!
$ahui
I teach linux.
test
案例5: 字符串序列加数字序列
[root@ahui ~]#grep '[a-zA-Z0-9]' ahui.txt
I am ahui!
$ahui
I teach linux.
test
$0 aaaa
$# bbbbbb
案例6: 匹配序列+特殊符号
[root@ahui ~]#grep '[:,$#.^a-zA-Z0-9]' ahui.txt
I am ahui!
$ahui
I teach linux.
test
$0 aaaa
$# bbbbbb
案例7: [] 中的特殊符号都是普通字符 相当于脱掉了马甲
[root@ahui ~]#grep '[.]$' ahui.txt
I teach linux.
not 0001.
[root@ahui ~]#grep '[!.]$' ahui.txt
I am ahui!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
not 0001.
[root@ahui ~]#grep '[\.]$' ahui.txt
I teach linux.
not 0001.
[^] 表示取反
^放在[]里的第一位时表示取反,第二位则为普通字符
案例: 除了包含.结尾的行 显示其他所有行
[root@ahui ~]#grep '[^.]$' ahui.txt
I am ahui!
$ahui
test
$0 aaaa
$# bbbbbb
案例: 以$ 或者 I 开头的行
[root@ahui ~]#grep '^[$I]' ahui.txt
I am ahui!
$ahui
I teach linux.
$0 aaaa
$# bbbbbb
I like badminton ball ,billiard ball and chinese chess!
案例:过滤文件中包含.和^的行
[root@ahui ~]#grep '[.^]' ahui.txt
I teach linux.
my blog is http: blog.51cto.com
our site is http:www.mhtnmsl.com
not 0001.
^^^^^^^^66$$$$$$$^^^$$
案例:对^和.取反
[root@ahui ~]#grep '[^^.]' ahui.txt
I am ahui!
$ahui
I teach linux.
test
$0 aaaa
基础正则的坑:
正则表达式第一坑: * 如果没有过滤到内容会显示所有的文件内容
正则表达式第二坑: .* 贪婪匹配 尽可能的匹配
正则表达式第三坑: [] 特殊含义的字符脱掉马甲 [^]表示取反
第一个扩展正则: +
grep支持扩展正则: egrep 或者 grep -E
作用: 前1个字符连续出现1次或1次以上的行
案例: 匹配8出现1次及1次以上的行
[root@ahui ~]#egrep '8+' ahui.txt -o
8
8
88888
案例: 匹配字母出现1次及1次以上的行
[root@ahui ~]#egrep 'ahui+' ahui.txt -o #匹配a出现1次及1次以上的行
ahui
ahui
ahui
ahui
ahui
第二个扩展正则: | 或者
案例: 查找包含ahui或者linux的行
[root@ahui ~]#egrep 'ahui|linux' ahui.txt
I am ahui!
I teach linux.
our site is http:www.mhtnmsl.com
ahuiahuiahui
或者
[root@ahui ~]#egrep 'ahu(i|o)' ahui.txt
I am ahui!
I am ahu0
our site is http:www.mhtnmsl.com
ahuiahuiahui
[root@ahui ~]#egrep 'ahui|ahuo|linux' ahui.txt
I am ahui!
I am ahuo
I teach linux.
企业案例: 查找文件中空行和# 并且取反
[root@ahui ~]#egrep -v '^$|^#' /etc/selinux/config
SELINUX=disabled
SELINUXTYPE=targeted
第三个扩展正则: {}
第一种方法: {n,m} 前一个字符至少出现n次 最多出现m次
案例: 8最少出现1次最多出现2次
[root@ahui ~]#egrep '8{1,2}' ahui.txt -o
8
8
88
88
8
案例: 8最少出现1次最多出现3次
[root@ahui ~]#egrep '8{1,3}' ahui.txt -o
8
8
888
88
{n} 前面最多显示n次
[root@ahui ~]#egrep '8{4}' ahui.txt -o
8888
[root@ahui ~]#
[root@ahui ~]#egrep '8{2}' ahui.txt -o
88
88
[root@ahui ~]#egrep '8{3}' ahui.txt -o
888
企业案例: 在文本中取出正确的身份证号码
身份特点:
总共18位
两种:
全为数字
17位+X
准备文档
李 2113421234
张 500224197
王 1233423423432ahui
万 5ahui
吕 lzy235872451234814
孔 150000123874591242
夏 222113859123487192
赵37142518322922103X
全为数字的身份证号
[root@ahui ~]#egrep '[0-9]{18}' id.txt
孔 150000123874591242
夏 222113859123487192
17位为数字 1位为X的
[root@ahui ~]#egrep '[0-9]{17}X' id.txt
赵 37142518322922103X
全为数字或者17位数字加一个X的身份证号
[root@ahui ~]#egrep '[0-9]{17}[0-9X]' id.txt
孔 150000123874591242
夏 222113859123487192
赵 37142518322922103X
不规范的身份证号
[root@ahui ~]#egrep '[0-9]{8}9[0-9]{8}[0-9X]' id.txt
夏 222113859123487192
用下面的字符处理不规范的身份证号
^ $ \b边界
[root@ahui ~]#egrep '[0-9]{17}[0-9X]' id.txt
孔 X150000123874591242
孔 1500001238745912421231qa
夏 222113859123487192
赵 37142518322922103X
拓展:
查看二进制文件
[root@ahui ~]#od /bin/sb
查看当前行最长行的字符串长度
[root@ahui ~]#echo aaa|wc -L
3
[root@ahui ~]#cat 1.txt
aa
bbb
cccc
a
[root@ahui ~]#cat 1.txt|wc -L
4
[root@ahui ~]#echo aaaaa|wc -L
5
重启网卡的命令:
[root@ahui ~]##ifdown eth0 && ifup #相当于执行 systemctl restarat network
[root@ahui ~]#ifdown eth0 && ifup eth0
find 反引号 `` 和$()相同
作用: 将执行后的结果留在原地 让其他命令调用
[root@ahui ~]#cp $(find /ahui/ -name "stu01.txt") /opt/
[root@ahui ~]#
[root@ahui ~]#ll /opt/
total 0
-rw-r--r-- 1 root root 0 May 13 15:44 stu01.txt
[root@ahui ~]#echo test
test
[root@ahui ~]#echo test.txt
test.txt
[root@ahui ~]#touch `echo test.txt`
[root@ahui ~]#ll
total 4
-rw-r--r-- 1 root root 14 May 13 15:01 1.txt
-rw-r--r-- 1 root root 0 May 13 15:45 test.txt
使用 cat -A 在行的最后面显示$
[root@ahui ~]#cat -A ahui.txt
I am ahui!$
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.mhtnmsl.com $
使用()将其中作为一个整体
[root@ahui ~]#egrep '(ahui)+' ahui.txt -o
ahui
ahui
ahuiahuiahui
[root@ahui ~]#cat 1.txt
root:x:0:0:root:/ahui:/bin/bash
扩展正则+的理解
+作用前面任意字符出现1次及1次以上 连续的作为1个整体
awk -F "[:/]+" '{print $6}' 1.txt #以:或\或:\作为分隔打印1.txt中第六列的字符
[root@b13k ~]#cat 1.txt
root:x:0:0:root:/ahui:/bin/bash
[root@b13k ~]#egrep ':/' 1.txt
root:x:0:0:root:/ahui:/bin/bash
[root@b13k ~]#egrep '[:/]' 1.txt
root:x:0:0:root:/ahui:/bin/bash
[root@b13k ~]#egrep '[:/]+' 1.txt
root:x:0:0:root:/ahui:/bin/bash
[root@b13k ~]#egrep '[:/]+' 1.txt -o
:
:
:
:
:/
:/
/
小结:
-
通配符:查找文件名称
* 表示所有
{} 序列
[] 区间范围 任意
? 任意单个字符通配符使用率最高的符号: * {} []
-
基础正则:
^ 以什么开头
$ 以什么结尾
^$ 表示空行
*前一个字符出现0次及1次以上
. 任意单个字符
.* 表示所有
[] 表示或者 在括号中的特殊符号都变成普通符号
[^] 取反
^[] 以扩号中任意单个字符开头
{n,m} 最少出现n最多出现m次
{n} 最多出现多少次 -
扩展正则:
+ 前1次字符出现1次及1次以上作为一个整体
| 或者
{n,m} 前一个字符至少出现n次 最多出现m次