利用sed 取出ifconfig命令中本机的IPv4地址

sed 常用选项:

编辑命令:
-d //删除模式空间内容
p //显示模式空间内容,-n(不显示)
a \Text //行后追加文本
i \Text //行前插入文本
c \Text //替换文本
w FileName //保存匹配的内容到指定路径文件中
r //读取指定路径文本至指定位置 //sed '6r /etc/fstab' /tmp/fstab
= //为匹配的内容打印行号
! //取反模式空间
s/查找模式空间内容/替换内容/g //查找替换全局(g表示每一行出现的所有)
替换标记:g 行内全局 p 显示替换成功的行 w 结果保存至指定文件
& //引用前面匹配内容

 

 

 

 

[root@centos ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.50.130 netmask 255.255.255.0 broadcast 192.168.50.255
inet6 fe80::20c:29ff:fe33:c9d4 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:33:c9:d4 txqueuelen 1000 (Ethernet)
RX packets 24622 bytes 34273572 (32.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4389 bytes 500399 (488.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 6 bytes 380 (380.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6 bytes 380 (380.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:c9:ca:ee txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

[root@centos ~]# ifconfig ens33 | sed -n '/inet /s#\(.*inet \)\(.*\)\( n.*\)#\2#gp'
192.168.50.130

posted @ 2022-01-29 22:06  LeeSin-  阅读(255)  评论(0编辑  收藏  举报