Bootstrap

SHELL脚本-通配符+正则表达式

SHELL脚本-通配符+正则表达式

通配符

一般用于文件名匹配

常用通配符

*:	匹配0或多个任意字符
?:	匹配任意一个字符

[list] : 匹配list中的任意单个字符
[c1-c2]: 匹配c1‐c2中任意单个字符
[!list]: 匹配除list中的任意单个字符
[^list]: 匹配除list中的任意单个字符

{string1,string2,...}:匹配string1,string2或更多字符串
{c1‐c2}: 匹配c1‐c2中任意多个字符
  • 用法
[root@server1 tmp]# touch file{1..8}
[root@server1 tmp]# ls
file1  file2  file3  file4  file5  file6  file7  file8
[root@server1 tmp]# rm -rf file*
[root@server1 tmp]# ls

[root@server1 tmp]# touch file{1,2,3}
[root@server1 tmp]# ls file[123]
file1  file2  file3
[root@server1 tmp]# touch file{a..f}
[root@server1 tmp]# ls file[a-f]
filea  fileb  filec  filed  filee  filef
[root@server1 tmp]# touch file{4,5,6}
[root@server1 tmp]# ls file[^456]
file1  file2  file3  filea  fileb  filec  filed  filee  filef

[root@server1 tmp]# touch file{10..15}
[root@server1 tmp]# ls
file10  file11  file12  file13  file14  file15

shell转义符

" ": 软转义,引号内部为整体,允许执行内部命令
' ': 硬转义,引号内部为整体
` `: 同$()一样,引号(括号)里的命令会优先执行,但若存在嵌套,反撇号不能用
  • 用法
[root@server1 tmp]# echo $(date +%F)
2021-08-21
[root@server1 tmp]# echo "$(hostname)"
server1
[root@server1 tmp]# echo '$(hostname)'
$(hostname)

正则表达式

概念

  • 正则表达式(Regular Expression、regex或regexp,缩写为RE),也译为正规表示法、常规表示法,是一种字符模式,用于在查找过程中匹配指定的字符。针对文件内容的文本过滤工具里,大都用到正则表达式,如vi,grep,awk, sed,find等
  • 元字符:在正则表达式中具有特殊意义的专用字符
  • 前导字符:位于元字符前面的字符

常用元字符

#示例文件
[root@server1 tmp]# cat test.txt 
hello
hello kugou

scjnhbjzj cbhx
xvjnk jnffjkdfjkvn  jfddnvfkvsi
scnjdsj
dvvre
192.168.138.14
10086/
sdffregkksdknaer;

www.baidui.com
www.taobao.com.

114.114.114.114
192.168.226.14/24

. : 任意单个字符
* : 前导字符出现0次或连续多次,ab*能匹配“a”,“ab”以及“abb”
.*: 任意长度的字符
[root@server1 tmp]# cat test.txt |grep sc.*
scjnhbjzj cbhx
scnjdsj

^ : 以什么开头
$ : 以什么结尾
^$:	空行
[root@server1 tmp]# cat test.txt |grep '^hel'
hello
hello kugou
[root@server1 tmp]# cat test.txt |grep 'ou$'
hello kugou

[list]  : 匹配list中的任意单个字符
[^list] : 匹配除list中的任意单个字符
^[list] : 匹配以list中的任意单个字符开头的
^[^list]: 匹配除list中的任意单个字符开头的
[root@server1 tmp]# cat test.txt |grep '[^abc]'
hello
hello kugou
scjnhbjzj cbhx
xvjnk jnffjkdfjkvn  jfddnvfkvsi
scnjdsj
dvvre
192.168.138.14
10086/
sdffregkksdknaer;
www.baidui.com
www.taobao.com.
114.114.114.114
192.168.226.14/24
[root@server1 tmp]# cat test.txt |grep '^[dcb]'
dvvre

\<	: 以什么开头
\>	: 以什么结尾
\<\>: 精确匹配
[root@server1 tmp]# cat test.txt |grep '\<hello'
hello
hello kugou
[root@server1 tmp]# cat test.txt |grep 'kugou\>'
hello kugou
[root@server1 tmp]# cat test.txt |grep '\<10086\>'
10086/

\{n\}  : 匹配前导字符连续出现n次 如:go\{2\},匹配goo... 
\{n,\} : 匹配前导字符至少出现n次 如:go\{2,\},匹配goo...,gooo...,goooo...,gooooo...等
\{n,m\}: 匹配前导字符出现n次与m次之间 如:go\{2,\},匹配goo...,gooo...

\(strings\) : 保存被匹配的字符,后面可以用标签\1代替
[root@server1 tmp]# sed 's/\(SELINUX=\)disabled/\1enforced/' /etc/selinux/config 
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     disabled - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of disabled.
#     disabled - No SELinux policy is loaded.
SELINUX=enforced
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 

扩展正则表达式(-E)

+ : 匹配一个或多个前导字符
? : 匹配零个或一个前导字符
[root@server1 tmp]# cat test.txt |grep -E 'h+'
hello
hello kugou
scjnhbjzj cbhx
 
a|b : 匹配a或b
()  : 组字符
[root@server1 tmp]# cat test.txt |grep -E '3|4'
192.168.138.14
114.114.114.114
192.168.226.14/24
[root@server1 tmp]# cat test.txt |grep -E '(taobao|baidui).com'
www.baidui.com
www.taobao.com.

{n}  : 前导字符重复n次	    \{n\}
{n,} : 前导字符重复至少n次  \{n,\}
{n,m}: 前导字符重复n到m次	\{n,m\}
[root@server1 tmp]# cat test.txt |grep -E '1{1,2}'
192.168.138.14
10086/
114.114.114.114
192.168.226.14/24

Perl内置正则(-P)

\d : 匹配数字  [0-9]
\w : 匹配字母数字下划线[a-zA-Z0-9_]
\s : 匹配空格、制表符、换页符[\t\r\n]
[root@server1 tmp]# cat test.txt |grep '[0-9]'
192.168.138.14
10086/
114.114.114.114
192.168.226.14/24
[root@server1 tmp]# cat test.txt |grep -P '\d'
192.168.138.14
10086/
114.114.114.114
192.168.226.14/24
[root@server1 tmp]# cat test.txt |grep -P '\s'
hello kugou
scjnhbjzj cbhx
xvjnk jnffjkdfjkvn  jfddnvfkvsi

综合案列

在test.txt文件中匹配ip地址
匹配IP地址
[root@server1 tmp]# cat test.txt |grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'
192.168.138.14
[root@server1 tmp]# cat test.txt |grep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
192.168.138.14
[root@server1 tmp]# cat test.txt |grep -P '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
192.168.138.14
[root@server1 tmp]# cat test.txt |grep -P '(\d{1,3}\.){3}\d{1,3}'
192.168.138.14

匹配本机IP地址、广播地址、子网掩码
[root@server1 tmp]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.226.10  netmask 255.255.255.0  broadcast 192.168.226.255
        inet6 fe80::1b78:bfb3:4567:b45c  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:17:9d:b3  txqueuelen 1000  (Ethernet)
        RX packets 13611  bytes 12551752 (11.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5499  bytes 485078 (473.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@server1 tmp]# ifconfig ens33|grep broadcast|grep -o -P '(\d{1,3}\.){3}\d{1,3}'
192.168.226.10
255.255.255.0
192.168.226.255
[root@server1 tmp]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 |grep -v 'DNS'|grep  -P '(\d{1,3}\.){3}\d{1,3}'
IPADDR=192.168.226.10
GATEWAY=192.168.226.2
NETMASK=255.255.255.0

了解:第二类正则

表达式功能备注
[:alnum:]字母与数字字符[[:alnum:]]=[a-zA-Z0-9]
[:alpha:]字母字符(包括大小写字母)[[:alpha:]]=[a-zA-Z]
[:blank:]空格与制表符[[:blank:]]=[\t\r]
[:digit:]数字[[:digit:]]=[0-9]
[:lower:]小写字母[[:lower:]]=[a-z]
[:upper:]大写字母[[:upper:]]=[A-Z]
[:punct:]标点符号
[:space:]包括换行符,回车等在内的所有空白[[:space:]]=[\t\r\n]=\s
;