3.請執行命令取出linux中eth0的IP地址(請用cut,awk,sed命令答)。
cut方法:
[root@ianLinux ~]# ifconfig eth0|sed -n '2p'
inet addr:192.168.0.199 Bcast:192.168.0.255 Mask:255.255.255.0
[root@ianLinux ~]# ifconfig eth0|sed -n '2p'|cut -d":" -f2|cut -d" " -f1
awk方法:
[root@ianLinux ~]# ifconfig eth0|sed -n '2p'
inet addr:192.168.0.199 Bcast:192.168.0.255 Mask:255.255.255.0
[root@ianLinux ~]# ifconfig eth0|sed -n '2p'|awk -F ":" '{print $2}'|awk '{print $1}'
awk多分隔符:
[root@ianLinux ~]# ifconfig eth0|awk -F '[: ]+' 'NR==2 {print $4}'
‘+’重複一個或多個前面的字符。
sed後向引用方法:
[root@ianLinux ~]# ifconfig eth0|sed -n '2p'
inet addr:192.168.0.199 Bcast:192.168.0.255 Mask:255.255.255.0
[root@ianLinux ~]# ifconfig eth0|sed -nr '2s#^.*dr:(.*) B.*$#\1#gp'
192.168.0.199
awk與sed配合使用
awk的過濾列,sed的替換
[root@ianLinux ~]# ifconfig eth0|awk '/inet addr/{print $2}'|sed 's#^.*:##g'
grep方法:
[root@ianLinux ~]# ifconfig eth0|grep -Eo '1[0-9]{2}\.[0-9]{1,3}\.[0-9]{1,3}\.1[0-9]{1,2}'
grep與cut配合方法:
[root@ianLinux ~]# grep IPADDR /etc/sysconfig/network-scripts/ifcfg-eth0|cut -d"=" -f2
grep與awk配合方法:
[root@ianLinux ~]# ifconfig eth0|grep "inet addr"|awk -F'[ :]+' '{print $4}'