Sed - 替换紧跟在特定模式之后的下一个字符串/单词 [英] Sed - Replace immediate next string/word coming after a particular pattern
本文介绍了Sed - 替换紧跟在特定模式之后的下一个字符串/单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 shell 脚本的新手,非常感谢任何帮助.
I'm a newbie to shell scripting and any help is much appreciated.
我有一个这样的模式 rmd_ver=1.0.10
我想搜索模式 rmd_ver=
并将数字部分 1.0.10
替换为所有匹配项中的新值.希望我的问题很清楚.
I want to search the pattern rmd_ver=
and replace the numeric part 1.0.10
with a new value in all the matches. Hope my question is clear.
推荐答案
替换任何值直到行尾:
sed -i 's/\(rmd_ver=\)\(.*\)/\1R/' file
sed -i 's/p/r/' file
用r
替换file
\(
开始第一组rmd_ver=
搜索模式\)
结束第一组\(
开始第二组.*
任意字符\)
结束第二组\1
回参考第一组R
替换文本sed -i 's/p/r/' file
replacep
withr
infile
\(
start first grouprmd_ver=
search pattern\)
end first group\(
start second group.*
any characters\)
end second group\1
back reference to the first groupR
replacement text
要在一行中的任何位置替换确切的模式,并且可能在一行中多次替换:
To replace the exact pattern in any place of the line and possibly several times in one line:
sed -i 's/\(rmd_ver=\)\(1\.0\.10\)/\1R/g' file
\.
将特殊的.
转义为文字.
g
替换一行中的多次出现\.
escape special.
into literal.
g
to replace multiple occurrences in one line
这篇关于Sed - 替换紧跟在特定模式之后的下一个字符串/单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文