How do I search and replace whole words using sed?
如何使用sed搜索和替换整个单词?
Doing
干
sed -i 's/[oldtext]/[newtext]/g' <file>
will also replace partial matches of [oldtext]
which I don't want it to do.
也将替换[oldtext]的部分匹配,我不希望它这样做。
6 个解决方案
#1
134
\b in regular expressions match word boundaries (i.e. the location between the first word character and non-word character):
正则表达式中的\ b匹配单词边界(即第一个单词字符和非单词字符之间的位置):
$ echo "bar embarassment" | sed "s/\bbar\b/no bar/g"
no bar embarassment
#2
110
On Mac OS X, neither of these regex syntaxes work inside sed for matching whole words
在Mac OS X上,这些正则表达式语法都不能在sed中用于匹配整个单词
\bmyWord\b
- \ bmyWord \ b
\<myWord\>
-
\
Hear me now and believe me later, this ugly syntax is what you need to use:
现在听我说,相信我,这个丑陋的语法是你需要使用的:
/[[:<:]]myWord[[:>:]]/
- / [[:<:]] myWord [[:>:]] /
So, for example, to replace mint with minty for whole words only:
因此,例如,仅使用minty替换mint用于整个单词:
sed "s/[[:<:]]mint[[:>:]]/minty/g"
- sed“s / [[:<:]] mint [[:>:]] / minty / g”
Source: re_format man page
来源:re_format手册页
#3
11
Use \b
for word boundaries:
使用\ b表示字边界:
sed -i 's/\boldtext\b/newtext/g' <file>
#4
7
In one of my machine, delimiting the word with "\b
" (without the quotes) did not work. The solution was to use "\<
" for starting delimiter and "\>
" for ending delimiter.
在我的一台机器中,用“\ b”(没有引号)分隔单词不起作用。解决方案是使用“\ <”开始分隔符,使用“\>”来结束分隔符。
To explain with Joakim Lundberg's example:
用Joakim Lundberg的例子来解释:
$ echo "bar embarassment" | sed "s/\<bar\>/no bar/g"
no bar embarassment
#5
2
in shell command:
在shell命令中:
echo "bar embarassment" | sed "s/\bbar\b/no bar/g"
or:
要么:
echo "bar embarassment" | sed "s/\<bar\>/no bar/g"
but if you are in vim, you can only use the later:
但如果你在vim,你只能使用后者:
:% s/\<old\>/new/g
#6
-2
$ echo "bar embarassment"|awk '{for(o=1;o<=NF;o++)if($o=="bar")$o="no bar"}1'
no bar embarassment
#1
134
\b in regular expressions match word boundaries (i.e. the location between the first word character and non-word character):
正则表达式中的\ b匹配单词边界(即第一个单词字符和非单词字符之间的位置):
$ echo "bar embarassment" | sed "s/\bbar\b/no bar/g"
no bar embarassment
#2
110
On Mac OS X, neither of these regex syntaxes work inside sed for matching whole words
在Mac OS X上,这些正则表达式语法都不能在sed中用于匹配整个单词
\bmyWord\b
- \ bmyWord \ b
\<myWord\>
-
\
Hear me now and believe me later, this ugly syntax is what you need to use:
现在听我说,相信我,这个丑陋的语法是你需要使用的:
/[[:<:]]myWord[[:>:]]/
- / [[:<:]] myWord [[:>:]] /
So, for example, to replace mint with minty for whole words only:
因此,例如,仅使用minty替换mint用于整个单词:
sed "s/[[:<:]]mint[[:>:]]/minty/g"
- sed“s / [[:<:]] mint [[:>:]] / minty / g”
Source: re_format man page
来源:re_format手册页
#3
11
Use \b
for word boundaries:
使用\ b表示字边界:
sed -i 's/\boldtext\b/newtext/g' <file>
#4
7
In one of my machine, delimiting the word with "\b
" (without the quotes) did not work. The solution was to use "\<
" for starting delimiter and "\>
" for ending delimiter.
在我的一台机器中,用“\ b”(没有引号)分隔单词不起作用。解决方案是使用“\ <”开始分隔符,使用“\>”来结束分隔符。
To explain with Joakim Lundberg's example:
用Joakim Lundberg的例子来解释:
$ echo "bar embarassment" | sed "s/\<bar\>/no bar/g"
no bar embarassment
#5
2
in shell command:
在shell命令中:
echo "bar embarassment" | sed "s/\bbar\b/no bar/g"
or:
要么:
echo "bar embarassment" | sed "s/\<bar\>/no bar/g"
but if you are in vim, you can only use the later:
但如果你在vim,你只能使用后者:
:% s/\<old\>/new/g
#6
-2
$ echo "bar embarassment"|awk '{for(o=1;o<=NF;o++)if($o=="bar")$o="no bar"}1'
no bar embarassment