找到匹配的文本并替换下一行 [英] find matching text and replace next line
本文介绍了找到匹配的文本并替换下一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在文件中查找一行并将下一行替换为特定值.我尝试使用sed,但似乎不喜欢\ n.还可以怎么做?
I'm trying to find a line in a file and replace the next line with a specific value. I tried sed, but it seems to not like the \n. How else can this be done?
文件如下:
<key>ConnectionString</key>
<string>anything_could_be_here</string>
我想将其更改为此
<key>ConnectionString</key>
<string>changed_value</string>
这是我尝试过的:
sed -i '' "s/<key>ConnectionString<\/key>\n<string><\/string>/<key>ConnectionString<\/key>\n<string>replaced_text<\/string>/g" /path/to/file
推荐答案
这可能对您有用(GNU sed):
This might work for you (GNU sed):
sed '/<key>ConnectionString<\/key>/!b;n;c<string>changed_value</string>' file
!b
否定上一个地址(regexp)并中断所有处理,结束sed命令,n
打印当前行,然后将下一行读入模式空间,c
将当前行更改为命令后面的字符串.
!b
negates the previous address (regexp) and breaks out of any processing, ending the sed commands, n
prints the current line and then reads the next into the pattern space, c
changes the current line to the string following the command.
这篇关于找到匹配的文本并替换下一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文