天天看點

老男孩教育每日一題-2017年3月29日-使用ifconfig取出網卡eth0的ip位址-看看你有多少方法

方法1:sed指令

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

<code>10.0.0.50</code>

方法2:cut

<code>[root@oldboyedu ~]</code><code># ifconfig eth0|grep 'inetaddr'|cut -d ":" -f2|cut -d " " -f1</code>

方法3:普通awk 使用2遍

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

方法4:awk同時多分隔符法

<code>[root@oldboyedu ~]</code><code># ifconfig eth0|grep 'inetaddr'|awk -F '[ :]' '{print $13}'</code>

方法5:awk同時多分隔符法

<code>[root@oldboyedu ~]</code><code># ifconfig eth0|sed -n '2p'|awk-F '[ :]+' '{print $4}'</code>

<code>[root@oldboyedu ~]</code><code># ifconfig eth0 |awk -F'[ :]+' 'NR==2 {print $4}'</code>

方法6:sed(正則)

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

方法7:grep-perl正則方法

<code>[root@show ~]</code><code># ifconfig eth0|grep -Po '(?&lt;=dr:)[0-9.]+'</code>

本文轉自 李導 51CTO部落格,原文連結:http://blog.51cto.com/lidao/1911571