[root@localhost ~]# ifconfig
eth0 Link encap:Ethernet HWaddr DA:DE:BE:41:89:18
inet addr:192.168.150.53 Bcast:192.168.150.255 Mask:255.255.255.0
inet6 addr: fe80::d8de:beff:fe41:8918/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:184255445 errors:0 dropped:0 overruns:0 frame:0
TX packets:51947 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:12701252470 (11.8 GiB) TX bytes:4422271 (4.2 MiB)
Interrupt:17
方法1:比對 inet addr 這行;以冒号為分隔符,顯示第2段;再次以空格為分隔符,顯示第1段
[root@localhost ~]# ifconfig eth0 |grep 'inet addr' |cut -d: -f2 |cut -d" " -f1
192.168.150.53
方法2:比對 inet addr 這行;以空替換從開頭到 addr: 的字元串;再次以空替換從Bcast到結尾的字元串
[root@localhost ~]# ifconfig eth0 |grep 'inet addr' |sed 's/^.*addr://g' |sed 's/Bcast.*$//g'
方法3:比對 inet addr 這行;以:或者空格作為分隔符,然後列印第4列
[root@localhost ~]# ifconfig eth0 |grep 'inet addr' |awk -F[:" "]+ '{print $4}'
方法4:比對 inet addr 這行;以:作為分隔符,列印第2列,然後再列印第一列
[root@localhost ~]# ifconfig eth0 |grep 'inet addr' |awk -F: '{print $2}' |awk '{print $1}'
方法5:
[root@localhost ~]# ifconfig eth0|sed -nr '2s#^.*addr:(.*) Bcast.*$#\1#g'p
方法6:
[root@localhost ~]# ifconfig eth0 |awk '/inet addr:/ {print $2}' |awk -F: '{print $2}'
方法7:
方法8:
[root@localhost ~]# ifconfig eth0 |awk 'NR==2 {print $2}' |awk -F: '{print $2}'
方法9:
[root@localhost ~]# ip add |awk -F '[ /]+' 'NR==8 {print $3}'
本文轉自 M四月天 51CTO部落格,原文連結:http://blog.51cto.com/msiyuetian/1733259,如需轉載請自行聯系原作者