bash脚本-正则表达式
目标
熟练正则表达式进行搜索或匹配对应内容
名词说明
正则表达式提供了一种便于查找特定内容的模式匹配机制。vim、grep和less命令都可以使用正则表达式。正则表达式自成体系,该语言有其自身的语法和规则。
实操演练
- 常用正则表达式
| 命令 | 说明 |
|---|---|
| ^\d+$ | 匹配非负数内容 |
| ^[0-9]*[1-9][0-9]*$ | 匹配正整数 |
| ^((-\d+) | (0+))$ |
| ^-[0-9]*[1-9][0-9]*$ | 匹配负整数 |
| ^-?\d+$ | 匹配整数 |
| ^\d+(.\d+)?$ | 非负浮点数 |
| ^(([0-9]+.[0-9]*[1-9][0-9])|([0-9][1-9][0-9].[0-9]+)|([0-9][1-9][0-9]*))$ | 正浮点数 |
| ^((-\d+(.\d+)?) | (0+(.0+)?))$ |
| ^(-(([0-9]+.[0-9]*[1-9][0-9])|([0-9][1-9][0-9].[0-9]+)|([0-9][1-9][0-9]*)))$ | 负浮点数 |
| ^(-?\d+)(.\d+)?$ | 浮点数 |
| ^[A-Za-z]+$ | 由26个英文字母组成的字符串 |
| ^[A-Z]+$ | 由26个英文字母的大写组成的字符串 |
| ^[a-z]+$ | 由26个英文字母的小写组成的字符串 |
| ^[A-Za-z0-9]+$ | 由数字和26个英文字母组成的字符串 |
| ^\w+$ | 由数字、26个英文字母或者下划线组成的字符串 |
| ^[\w-]+(.[\w-]+)*@[\w-]+(.[\w-]+)+$ | email地址 |
| ^[a-zA-z]+://(\w+(-\w+))(.(\w+(-\w+)\))(?\S)?$ | URL |
| ^(d{2}|d{4})-((0([1-9]{1}))|(1[1|2]))-((0-2)|(3[0|1]))$ | 年-月-日 |
| ^((0([1-9]{1}))|(1[1|2]))/((0-2)|(3[0|1]))/(d{2}|d{4})$ | 月/日/年 |
| ^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$ | Emil |
| ^((+?[0-9]{2,4}-[0-9]{3,4}-)|([0-9]{3,4}-))?([0-9]{7,8})(-[0-9]+)?$ | 电话号码 |
| ^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$ | IP地址 |
| \n[\s| ]*\r | 匹配空行 |
| (^\s)|(\s$) | 匹配首尾空格 |
| ^[a-zA-z]+://(\w+(-\w+))(\.(\w+(-\w+)))(\?\S)?$ | 匹配URL |
| (\d{3}-|\d{4}-)?(\d{8}|\d{7})? | 匹配国内电话号码 |
- 命令:
grep语法:grep [-abcEFGhHilLnqrsvVwxy][-A<显示行数>][-B<显示列数>][-C<显示列数>][-d<进行动作>][-e<范本样式>][-f<范本文件>][–help][范本样式][文件或目录…]
-a 或 –text : 不要忽略二进制的数据。
-A<显示行数> 或 –after-context=<显示行数> : 除了显示符合范本样式的那一列之外,并显示该行之后的内容。
-b 或 –byte-offset : 在显示符合样式的那一行之前,标示出该行第一个字符的编号。
-B<显示行数> 或 –before-context=<显示行数> : 除了显示符合样式的那一行之外,并显示该行之前的内容。
-c 或 –count : 计算符合样式的列数。
-C<显示行数> 或 –context=<显示行数>或-<显示行数> : 除了显示符合样式的那一行之外,并显示该行之前后的内容。
-d <动作> 或 –directories=<动作> : 当指定要查找的是目录而非文件时,必须使用这项参数,否则grep指令将回报信息并停止动作。
-e<范本样式> 或 –regexp=<范本样式> : 指定字符串做为查找文件内容的样式。
-E 或 –extended-regexp : 将样式为延伸的正则表达式来使用。
-f<规则文件> 或 –file=<规则文件> : 指定规则文件,其内容含有一个或多个规则样式,让grep查找符合规则条件的文件内容,格式为每行一个规则样式。
-F 或 –fixed-regexp : 将样式视为固定字符串的列表。
-G 或 –basic-regexp : 将样式视为普通的表示法来使用。
-h 或 –no-filename : 在显示符合样式的那一行之前,不标示该行所属的文件名称。
-H 或 –with-filename : 在显示符合样式的那一行之前,表示该行所属的文件名称。
-i 或 –ignore-case : 忽略字符大小写的差别。
-l 或 –file-with-matches : 列出文件内容符合指定的样式的文件名称。
-L 或 –files-without-match : 列出文件内容不符合指定的样式的文件名称。
-n 或 –line-number : 在显示符合样式的那一行之前,标示出该行的列数编号。
-o 或 –only-matching : 只显示匹配PATTERN 部分。
-q 或 –quiet或–silent : 不显示任何信息。
-r 或 –recursive : 此参数的效果和指定”-d recurse”参数相同。
-s 或 –no-messages : 不显示错误信息。
-v 或 –invert-match : 显示不包含匹配文本的所有行。
-V 或 –version : 显示版本信息。
-w 或 –word-regexp : 只显示全字符合的列。
-x –line-regexp : 只显示全列符合的列。
-y : 此参数的效果和指定”-i”参数相同。
- 演示实例
[root@sinfotek ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
##匹配root的内容
[root@sinfotek ~]# grep ^root /etc/passwd
root:x:0:0:root:/root:/bin/bash
##匹配root开头的内容
[root@sinfotek ~]# grep bash$ /etc/passwd
root:x:0:0:root:/root:/bin/bash
sinfotek:x:1000:1000:sinfotek:/home/sinfotek:/bin/bash
##匹配bash结尾的内容
[root@sinfotek ~]# grep r..t /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
##匹配rt之间任意两个字符的内容
[root@sinfotek ~]# grep ro*t /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
##匹配rt之间存在任意0个的内容
[root@sinfotek ~]# grep "ro\{2\}t" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
##匹配o的倍数的内容
[root@sinfotek ~]# grep r.*t /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
libstoragemgmt:x:997:994:daemon account for libstoragemgmt:/var/run/sm:/sbin/nologin
gluster:x:995:992:GlusterFS daemons:/run/gluster:/sbin/nologin
saslauth:x:994:76:Saslauthd user:/run/saslauthd:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
setroubleshoot:x:993:990::/var/lib/setroubleshoot:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
unbound:x:991:988:Unbound DNS resolver:/etc/unbound:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsddaemon:/dev/null:/sbin/nologin
gnome-initial-setup:x:988:982::/run/gnome-initial-setup/:/sbin/nologn
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
##匹配rt之间任意字符的内容
[root@sinfotek ~]# grep ro[oux]t /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
##匹配root rout roxt 的内容
[root@sinfotek ~]# grep -v -e '#' -e '^$' /etc/ssh/sshd_config
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
SyslogFacility AUTHPRIV
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication yes
ChallengeResponseAuthentication no
GSSAPIAuthentication yes
GSSAPICleanupCredentials no
UsePAM yes
X11Forwarding yes
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
Subsystem sftp /usr/libexec/openssh/sftp-server
##过滤配置文件中的内容