[英]Using Sed to insert file contents where pattern is matched
I have a file called source.txt which contains the following lines:-我有一个名为 source.txt 的文件,其中包含以下几行:-
the
quick
brown
fox
jumped
over
the
lazy
dog
I want to search for the 'fox' line and delete it and all lines that follow, up to and including the 'over' line.我想搜索 'fox' 行并删除它和后面的所有行,直到并包括 'over' 行。 Then I want to insert the contents of another file (fruit.txt) in place of those lines.然后我想插入另一个文件 (fruit.txt) 的内容来代替这些行。 Fruit contains:-水果包含:-
apples
bananas
pears
So the result should be a file that looks like this:-所以结果应该是一个看起来像这样的文件:-
the
quick
brown
apples
bananas
pears
the
lazy
dog
I think this will be possible to do using the sed command.我认为使用 sed 命令可以做到这一点。
This command successfully removes the lines:-此命令成功删除了这些行:-
sed -i -e '/fox/,/over/d' source.txt
And this command almost works, but inserts the contents of the fruit.txt file three times, once for each matching line:-这个命令几乎可以工作,但插入了fruit.txt文件的内容三次,每个匹配行一次:-
sed -i -e '/fox/,/over/{r fruit.txt' -e 'd}' source.txt
Any help would be greatly appreciated!任何帮助将不胜感激!
In this condition you have to delete the undesired one by this command在这种情况下,您必须通过此命令删除不需要的
sed -i -e '/fox/,/over/d' source.txt
Then add the required one by this command然后通过此命令添加所需的
sed -i -e '/brown/ r fruit.txt' source.txt
I hope it will help you我希望它会帮助你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.