sed 全词搜索和替换 [英] sed whole word search and replace

查看:50
本文介绍了sed 全词搜索和替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 sed 搜索和替换整个单词?

How do I search and replace whole words using sed?

sed -i 's/[oldtext]/[newtext]/g' <file> 

还将替换我不希望它做的 [oldtext] 的部分匹配.

will also replace partial matches of [oldtext] which I don't want it to do.

推荐答案

\b 在正则表达式中匹配单词边界(即第一个单词字符和非单词字符之间的位置):

\b in regular expressions match word boundaries (i.e. the location between the first word character and non-word character):

$ echo "bar embarassment" | sed "s/\bbar\b/no bar/g"
no bar embarassment

这篇关于sed 全词搜索和替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆