|
13、egrep和扩展的正则表达式
2021年07月13日 |
|
13.1 grep REGEXP:REGular EXPression(正则表达式) Pattern: (模式) 1、正则表达式: Basic REGEXP:基本 Extended REGEXP:扩展 2、基本正则表达式: .: 任意词 []: 括号中间的任意单个字符 [^]:反向选取 3、次数匹配: *: 其前的字符匹配任意次 \?: 0或1次 \{m,n\}:至少m次,至多n次; .*: 任意长度的任意字符 4、锚定: ^:行首 $:行尾 \<, \b:单词首 \>, \b:单词尾 \(\):用于实现分组引用 \1, \2, \3, …(引用前面第n个小括号里面的内容) grep:使用基本正则表达式定义的模式来过滤文本的命令(默认只支持基本正则表达式); -i:忽略大小写 -v:反向搜索 -o:只显示匹配到的字符串 –color:显示颜色 -E: 使用扩展正则表达式 -A #: 显示匹配到的某一行后面的第n行 -B #: 显示匹配到的某一行前面的第n行 -C #:显示匹配到的某一行前后面各n行 [root@Daniel-R480 ~]# grep -A 2 ‘^core id’ /proc/cpuinfo core id : 0 cpu cores : 4 apicid : 0 [root@Daniel-R480 ~]# grep -B 2 ‘^core id’ /proc/cpuinfo physical id : 0 siblings : 8 core id : 0 [root@Daniel-R480 ~]# grep -C 2 ‘^core id’ /proc/cpuinfo physical id : 0 siblings : 8 core id : 0 cpu cores : 4 apicid : 0 — 13.2 扩展正则表达式: 字符前面默认不加\ 1、字符匹配: . [] [^] 2、次数匹配: *: ?: +: 匹配其前面的字符至少1次 {m,n} 3、位置锚定: ^ $ \< \> 4、分组: ():分组 \1, \2, \3, … 5、或者 |: or C|cat: C或cat(匹配的是C或者cat,匹配左边或者右边的内容) (C|c)at:Cat或者cat [root@Daniel-R480 ~]# grep -E –color ‘C|cat’ test3.txt cat Cat C [root@Daniel-R480 ~]# grep -E ‘C|cat’ test3.txt cat Cat C [root@Daniel-R480 ~]# grep -E ‘(C|c)at’ test3.txt cat Cat [root@Daniel-R480 ~]# cat ./test3.txt cat Cat C c [root@Daniel-R480 ~]# grep -E = egrep 练习 1、显示所有以数字结尾且文件名中不包含空白的文件; ls *[^[:space:]]*[0-9] ????????? \.让元字符表示它本身的意义 找出/boot/grub/grub.conf文件中1-255之间的数字; [root@Daniel-R480 ~]# egrep ‘\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>’ /boot/grub/grub.conf title CentOS Linux 7 (3.10.0-957.1.3.el7.x86_64) kernel /boot/vmlinuz-3.10.0-957.1.3.el7.x86_64 ro root=UUID=f41e390f-835b-4223-a9bb-9b45984ddf8d console=hvc0 LANG=en_US.UTF-8 initrd /boot/initramfs-3.10.0-957.1.3.el7.x86_64.img [root@Daniel-R480 ~]# ifconfig | egrep ‘\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>’ inet 192.168.18.116 netmask 255.255.255.0 broadcast 192.168.18.255 inet www.danios.com netmask 255.0.0.0 [root@Daniel-R480 ~]# ifconfig | egrep –color ‘(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.){3}\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>’ inet 192.168.18.116 netmask 255.255.255.0 broadcast 192.168.18.255 inet www.danios.com netmask 255.0.0.0 [root@Daniel-R480 ~]# ifconfig | egrep ‘\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>’ inet 192.168.18.116 netmask 255.255.255.0 broadcast 192.168.18.255 ether e8:6a:64:86:12:9f (Ethernet) lo: flags=73<UP,LOOPBACK,RUNNING> mtu 1500 inet www.danios.com netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0xfe<compat,link,site,host> [root@Daniel-R480 ~]# ifconfig | egrep –color -o ‘(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2 [0-4][0-9]|25[0-5])\>\.){3}\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>’ 192.168.18.116 255.255.255.0 192.168.18.255 www.danios.com 255.0.0.0 2、只输出ifconfig里面的ip地址 IPv4: 5类:A B C D E A:1-127 B:128-191 C:192-223 只显示ip地址 [root@Daniel-R480 ~]# ifconfig | egrep ‘\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[01][0-9]|22[0-3])\>(\.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\>){2}\.\<([1-9]|[1-9][0-9] |1[0-9][0-9]|2[0-4][0-9]|25[0-4])\>’ inet 192.168.18.116 netmask 255.255.255.0 broadcast 192.168.18.255 inet www.danios.com netmask 255.0.0.0 [root@Daniel-R480 ~]# ifconfig | egrep -o ‘\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[01][0-9]|22[0-3])\>(\.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\>){2}\.\<([1-9]|[1-9][0 -9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\>’ 192.168.18.116 www.danios.com [root@Daniel-R480 ~]# |