使用ifconfig取出網卡eth0的ip地址-看看你有多少方法 ?

方法1:awk使用兩遍

[root@oldboy ~]# ifconfig eth0|awk 'NR==2 {print $2}'|awk -F ":" '{print $2}'
10.0.0.5

方法2:hostname命令

[root@oldboy ~]# hostname -I
10.0.0.5

方法3:cut命令

[root@oldboy ~]# ifconfig eth0|grep 'inet addr'|cut -d ":" -f2|cut -d " " -f1
10.0.0.5

方法4:sed命令

[root@oldboy ~]# ifconfig eth0|sed -n '2p'|sed 's#^.*addr:##g'|sed 's#  B.*$##g'
10.0.0.5

方法5:awk多分割符

[root@oldboy ~]# ifconfig eth0|awk "NR==2"|awk -F '[ :]' '{print $13}'
10.0.0.5

[root@oldboy ~]# ifconfig eth0|sed -n '2p'|awk -F '[ :]+' '{print $4}'
10.0.0.5

[root@oldboy ~]# ifconfig eth0 |awk -F'[ :]+' 'NR==2 {print $4}'
10.0.0.5

方法6:sed(正則)

[root@oldboy ~]# ifconfig eth0|sed -nr '2s#^.*dr:(.*) B.*$#\1#gp'
10.0.0.5

方法:7:grep-perl正則方法

[root@oldboy ~]# ifconfig eth0|grep -Po '(?<=dr:)[0-9.]+'
10.0.0.5
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章