[root@desktop3 ~]# cat test
12.12..12
01.02.02
255.255.255
255.256.254
144.144.144.144
255.255.255.255
255.255.256.254
01.01.01.01
001.001.001.024
222.222.222.222.2
[root@desktop3 ~]# grep -E -o "^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$" ip
[root@desktop3 ~]# grep -E -o '^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[0-9]{2}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[0-9]{2}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[0-9]{2}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[0-9]{2}|[1-9])$' test
[root@desktop3 ~]# grep -E -o "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$" test
使用选项 -E
grep -E 选项可以用来扩展选项为正则表达式。 如果使用了grep 命令的选项-E,则应该使用 | 来分割多个pattern,以此实现OR操作。