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分支:4 个月前 )
186a802e
added ecosystem file for PM2 5 年前
5def40a3
Add host customization support for the NodeJS version 5 年前
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐


所有评论(0)