方法1:sed命令

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

方法2cut

1
2
[root@oldboyedu ~] # ifconfig eth0|grep 'inetaddr'|cut -d ":" -f2|cut -d " " -f1
10.0.0.50

方法3:普通awk 使用2

1
2
[root@oldboyedu ~] # ifconfig eth0|grep 'inet addr'|awk -F ":" '{print $2}'|awk '{print $1}'
10.0.0.50

方法4awk同时多分隔符法

1
2
[root@oldboyedu ~] # ifconfig eth0|grep 'inetaddr'|awk -F '[ :]' '{print $13}'
10.0.0.50

方法5awk同时多分隔符法

1
2
3
4
[root@oldboyedu ~] # ifconfig eth0|sed -n '2p'|awk-F '[ :]+' '{print $4}'
10.0.0.50
[root@oldboyedu ~] # ifconfig eth0 |awk -F'[ :]+' 'NR==2 {print $4}'
10.0.0.50

方法6sed(正则)

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

方法7grep-perl正则方法

1
2
[root@show ~] # ifconfig eth0|grep -Po '(?<=dr:)[0-9.]+'
10.0.0.50



转载自http://lidao.blog.51cto.com/3388056/1911571