Linux中过滤IP地址的9种方法


[root@localhost ~]# ifconfig

eth0      Link encap:Ethernet  HWaddr DA:DE:BE:41:89:18

          inet addr:192.168.150.53  Bcast:192.168.150.255  Mask:255.255.255.0

          inet6 addr: fe80::d8de:beff:fe41:8918/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:184255445 errors:0 dropped:0 overruns:0 frame:0

          TX packets:51947 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:12701252470 (11.8 GiB)  TX bytes:4422271 (4.2 MiB)

          Interrupt:17


方法1:匹配 inet addr 这行;以冒号为分隔符,显示第2段;再次以空格为分隔符,显示第1段

[root@localhost ~]# ifconfig eth0 |grep 'inet addr' |cut -d: -f2 |cut -d" " -f1

192.168.150.53

方法2:匹配 inet addr 这行;以空替换从开头到 addr: 的字符串;再次以空替换从Bcast到结尾的字符串

[root@localhost ~]# ifconfig eth0 |grep 'inet addr' |sed 's/^.*addr://g' |sed 's/Bcast.*$//g'

192.168.150.53

方法3:匹配 inet addr 这行;以:或者空格作为分隔符,然后打印第4列

[root@localhost ~]# ifconfig eth0 |grep 'inet addr' |awk -F[:" "]+ '{print $4}'

192.168.150.53

方法4:匹配 inet addr 这行;以:作为分隔符,打印第2列,然后再打印第一列

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

192.168.150.53

方法5:

[root@localhost ~]# ifconfig eth0|sed -nr '2s#^.*addr:(.*) Bcast.*$#\1#g'p

192.168.150.53

方法6:

[root@localhost ~]# ifconfig eth0 |awk '/inet addr:/ {print $2}' |awk -F: '{print $2}'

192.168.150.53

方法7:

[root@localhost ~]# ifconfig eth0 |awk '/inet addr:/ {print $2}' |awk -F: '{print $2}'

192.168.150.53

方法8:

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

192.168.150.53

方法9:

[root@localhost ~]# ip add |awk -F '[ /]+' 'NR==8 {print $3}'

192.168.150.53





      本文转自 M四月天 51CTO博客,原文链接:http://blog.51cto.com/msiyuetian/1733259,如需转载请自行联系原作者



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值