linux正則表達式之取ip

使用ifconfig取出網卡eth0ip地址

方法1sed

[root@ZHOUQIZHONG ~]# ifconfig eth0 |sed -nr'2s#.*dr:|Bca.*##gp'

10.0.0.200 

[root@ZHOUQIZHONG ~]#

方法2sed

[root@ZHOUQIZHONG ~]# ifconfig eth0 |sed -nr'2s#.*dr:(.*)Bca.*#\1#gp'

10.0.0.200 

[root@ZHOUQIZHONG ~]#

方法3sed+cut

[root@ZHOUQIZHONG ~]# ifconfig eth0 |sed -n'2p'|cut -d ':' -f2 |cut -d ' ' -f1

10.0.0.200

[root@ZHOUQIZHONG ~]#

方法4awk

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

10.0.0.200

[root@ZHOUQIZHONG ~]#

方法5awk

[root@ZHOUQIZHONG ~]# ifconfig eth0 |awk -F'addr:|Bcast:' 'NR==2{print $2}'

10.0.0.200 

[root@ZHOUQIZHONG ~]#

......

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<一天變一個樣

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章