linux中使用文本工具截取ip 的几种方法
1,使用awk工具
[root@mycentos data]# ifconfig ens33 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.140 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::8abb:80e7:a0ad:6463 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:52:25:a4 txqueuelen 1000 (Ethernet) RX packets 53417 bytes 5526762 (5.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 4618 bytes 627215 (612.5 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@mycentos data]# ifconfig |awk -F " " 'NR==2 {print $2}' 192.168.1.140
2,使用sed工具
1)
[root@mycentos data]# ifconfig ens33 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.140 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::8abb:80e7:a0ad:6463 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:52:25:a4 txqueuelen 1000 (Ethernet) RX packets 53417 bytes 5526762 (5.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 4618 bytes 627215 (612.5 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@mycentos data]# ifconfig |sed -n "2p"|sed 's#^.*net ##g'|sed 's# net.*$##g' 192.168.1.140 [root@mycentos data]#
2)
[root@mycentos data]# ifconfig ens33 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.140 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::8abb:80e7:a0ad:6463 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:52:25:a4 txqueuelen 1000 (Ethernet) RX packets 53417 bytes 5526762 (5.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 4618 bytes 627215 (612.5 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@mycentos data]# ifconfig |sed -n "2p"|sed 's#^.*net ##g'|sed 's# net.*$##g' 192.168.1.140 [root@mycentos data]# 欢迎大家留意讨论 相互学习 我一直在
转载于:https://blog.51cto.com/1167248/2132389