- A+
所属分类:运维每日一题
方法1:sed命令
1 2 | [root@localhost html]# ifconfig em3|sed -n '2p'|sed 's#^.*addr:##g'|sed 's# B.*$##g' 202.75.223.202 |
方法2:cut
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 |
方法4:awk同时多分隔符法
1 2 | [root@oldboyedu ~]# ifconfig eth0|grep 'inetaddr'|awk -F '[ :]' '{print $13}' 10.0.0.50 |
方法5:awk同时多分隔符法
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 |
方法6:sed(正则)
1 | [root@oldboyedu ~]# ifconfig eth0 |sed -nr '2s#^.*dr:(.*) B.*$#\1#gp' 10.0.0.50 |
方法7:grep-perl正则方法
1 2 | [root@show ~]# ifconfig eth0|grep -Po '(?<=dr:)[0-9.]+' 10.0.0.50 |
注解本篇目前记录的方法适用于老系统centos6自带的ifconfig,而非centos7 net-tools中的同名命令,centos7系列的方法不久之后会给出
今天是老男孩教育每日一题陪伴大家的第17天。
对于题目和答案的任何疑问,请在博客评论区留言。
往期题目索引

我的微信公众号
我的微信公众号扫一扫