取出IP地址首先需要在文件中定位到有IP地址的那一行,所以我们先定位:
1.使用sed定位:
[root@oldboyedu39 ~]# ifconfig eth0 | sed -n ‘2p‘ inet addr:10.0.0.200 Bcast:10.0.0.255 Mask:255.255.255.0
2.使用awk定位:
[root@oldboyedu39 ~]# ifconfig eth0 | awk ‘NR==2‘ inet addr:10.0.0.200 Bcast:10.0.0.255 Mask:255.255.255.0
3.使用grep定位:
[root@oldboyedu39 ~]# ifconfig eth0 | grep "inet " inet addr:10.0.0.200 Bcast:10.0.0.255 Mask:255.255.255.0
定位完成以后我们就需要取出IP地址
[root@oldboyedu39 ~]# ifconfig eth0 | awk ‘NR==2‘ | cut -d " " -f12 | cut -d ":" -f2 10.0.0.200
[root@oldboyedu39 ~]# ifconfig eth0 | awk ‘NR==2‘ | cut -c 21-31 10.0.0.200
在网卡配置文件中取IP:
[root@oldboyedu39 ~]# awk ‘NR==8‘ /etc/sysconfig/network-scripts/ifcfg-eth0 | cut -d "=" -f2 10.0.0.200
[root@oldboyedu39 ~]# ifconfig eth0 | awk ‘NR==2‘ | awk ‘{print $2}‘ | awk -F ":" ‘{print $2}‘ 10.0.0.200
[root@oldboyedu39 ~]# ifconfig eth0 | awk ‘NR==2‘ | awk -F "[ :]" ‘{print $13}‘ 10.0.0.200
[root@oldboyedu39 ~]# ifconfig eth0 | awk ‘NR==2‘ | awk -F "[ :]+" ‘{print $4}‘ 10.0.0.200
[root@oldboyedu39 ~]# ifconfig eth0 | awk -F "[ :]+" ‘NR==2{print $4}‘ 10.0.0.200
[root@oldboyedu39 ~]# ifconfig eth0 | awk -F "addr:|Bcast:" ‘NR==2{print $2}‘ 10.0.0.200
也可以直接到网卡的配置文件中取IP:
[root@oldboyedu39 ~]# awk ‘/^IPADDR/‘ /etc/sysconfig/network-scripts/ifcfg-eth0 |awk -F "=" ‘{print $2}‘ 10.0.0.200
[root@oldboyedu39 ~]# awk -F "=" ‘/^IPADDR/{print $2}‘ /etc/sysconfig/network-scripts/ifcfg-eth0 10.0.0.200
[root@oldboyedu39 ~]# awk -F "=" ‘NR==8{print $2}‘ /etc/sysconfig/network-scripts/ifcfg-eth0 10.0.0.200
[root@oldboyedu39 ~]# ifconfig eth0 | sed -n ‘2p‘ | sed ‘s#^.*dr:##g‘ | sed ‘s#B.*##g‘ 10.0.0.200
[root@oldboyedu39 ~]# ifconfig eth0 | sed -n ‘2p‘ | sed -r ‘s#^.*dr:(.*)B.*$#\1#g‘ 10.0.0.200
[root@oldboyedu39 ~]# grep "IPADDR" /etc/sysconfig/network-scripts/ifcfg-eth0 | sed ‘s#^I.*=##g‘ 10.0.0.200
[root@oldboyedu39 ~]# grep "IPADDR" /etc/sysconfig/network-scripts/ifcfg-eth0 | awk -F "=" ‘{print $2}‘ 10.0.0.200
本文出自 “奋斗的牛犊子” 博客,请务必保留此出处http://niuduzi.blog.51cto.com/12092006/1955234
原文地址:http://niuduzi.blog.51cto.com/12092006/1955234