Linux命令練習第三關(2)

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}'

這裏寫圖片描述

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