linux三剑客实战

1.sed取ip地址

①传统写法

[root@localhost cs]# ifconfig eth0 | sed -n '2p'
          inet addr:192.168.197.150  Bcast:192.168.197.255  Mask:255.255.255.0
[root@localhost cs]# ifconfig eth0 | sed -n '2p'|sed 's/^.*dr://g'
192.168.197.150  Bcast:192.168.197.255  Mask:255.255.255.0
[root@localhost cs]# ifconfig eth0 | sed -n '2p'|sed 's/^.*dr://g'|sed 's/  Bcast.*$//g' 
192.168.197.150
[root@localhost cs]# 


sed -rn :
-r代表拓展正则,后面不需要转义字符 \ 来对有歧义的特殊符号进行转义,后面可以接拓展正则表达式
-n 取消默认输出
^.dr以任意字符开头匹配到dr结束
Bcast.
$ 以Bcast起始一直到最后结束
\1 固定写法,代表(.*)的内容

[root@localhost cs]# ifconfig|sed -rn 's/^.*dr:(.*) Bcast.*$/\1/gp'
192.168.197.150 
[root@localhost cs]#

③利用ifconfig eth0|sed -n '/inet addr:19/p’过滤出来这一行,然后再用/s###g/进行替换

[root@localhost ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:7C:5B:79  
          inet addr:192.168.197.150  Bcast:192.168.197.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe7c:5b79/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:87822 errors:0 dropped:0 overruns:0 frame:0
          TX packets:63769 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:72874461 (69.4 MiB)  TX bytes:27158178 (25.9 MiB)

[root@localhost ~]# ifconfig eth0|sed -nr '/inet addr:19/s#^.*:(.*) Bca.*$#\1#p'
192.168.197.150 
[root@localhost ~]#

2.awk

注意:print外面的’{ }’ 必须存在

[root@localhost ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:7C:5B:79  
          inet addr:192.168.197.150  Bcast:192.168.197.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe7c:5b79/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:87353 errors:0 dropped:0 overruns:0 frame:0
          TX packets:63516 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:72824853 (69.4 MiB)  TX bytes:27110624 (25.8 MiB)
[root@localhost ~]# ifconfig eth0| awk -F '[:]' 'NR==2{print$2}'|awk -F'[ ]' '{print$1}'  
192.168.197.150
[root@localhost ~]# ifconfig eth0| awk -F '[ :]+' 'NR==2{print$4}'                      
192.168.197.150

在这里插入图片描述
在这里插入图片描述
图片链接地址文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值