sed配合正则表达式应用案例
linux-dash
A beautiful web dashboard for Linux
项目地址:https://gitcode.com/gh_mirrors/li/linux-dash
·
问题:利用linux正则表达式把IP取出来。
解答:
[root@ianLinux ~]# ifconfig eth0|sed -n '2p'
inet addr:192.168.0.199 Bcast:192.168.0.255 Mask:255.255.255.0
(1)利用sed的替换功能替换:
sed ‘s#想要修改的内容#新内容#g’ 文件 。
把IP前面的inet addr:替换为空,把IP后面的那部分也替换为空。即可取出IP。
^.*以任意多个字符开头。.*$以任意多个字符结尾
[root@ianLinux ~]# ifconfig eth0|sed -n '2p'|sed 's#^.*dr:##g'
[root@ianLinux ~]# ifconfig eth0|sed -n '2p'|sed 's#^.*dr:##g'|sed 's# B.*$##g'
192.168.0.199
(2)利用sed的后向引用。
sed -r ‘s#(可以用正则匹配)#\1#g’ 文件。sed利用后向引用功能时要带-r参数。
1.s#^.*dr:(.*)#\1#g与s#(.*) B.*$#\1#g
[root@ianLinux ~]# ifconfig eth0|sed -n '2p'|sed -r 's#^.*dr:(.*)#\1#g'|sed -r 's#(.*) B.*$#\1#g'
2.s#^.*dr:(.*) B.*$#\1#g
[root@ianLinux ~]# ifconfig eth0|sed -n '2p'|sed -r 's#^.*dr:(.*) B.*$#\1#g'
3.2s#^.*dr:(.*) B.*$#\1#gp
[root@ianLinux ~]# ifconfig eth0|sed -nr '2s#^.*dr:(.*) B.*$#\1#gp'
A beautiful web dashboard for Linux
最近提交(Master分支:1 个月前 )
186a802e
added ecosystem file for PM2 5 年前
5def40a3
Add host customization support for the NodeJS version 5 年前
新一代开源开发者平台 GitCode,通过集成代码托管服务、代码仓库以及可信赖的开源组件库,让开发者可以在云端进行代码托管和开发。旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。
更多推荐


所有评论(0)