sed 只替换完全匹配 [英] Sed replace only exact match
本文介绍了sed 只替换完全匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不想在文件中用 Europe12_yesturday
替换像 Europe12
这样的字符串.不更改文件中同样存在的 Europe12-36
字符串.
I wan't to replace a string like Europe12
with Europe12_yesturday
in a file. Without changing the Europe12-36
strings that also exists in the file.
我试过了:
$basename=Europe12
sed -i 's/\b$basename\b/${basename}_yesterday/g' file.txt
但这也改变了 Europe12-36
字符串.
but this also changed the Europe12-36
strings.
推荐答案
手动构建您想要的分隔符列表,而不是使用 \b
、\W
或 \<
.-
不是单词字符(字母数字)的一部分,因此这也与您的其他字符串匹配.所以尝试这样的事情,根据需要扩展列表:[-a-zA-Z0-9]
.
Manually construct the delimiter list you want instead of using \b
, \W
or \<
. -
is not part of the word characters (alphanumericals), so that's why this also matches your other string. So try something like this, expanding the list as needed: [-a-zA-Z0-9]
.
这篇关于sed 只替换完全匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文