用存储在sed中的变量的文件内容替换两行之间的文本 [英] Replace text between two lines with contents of a file stored in a variable in sed

查看:548
本文介绍了用存储在sed中的变量的文件内容替换两行之间的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为original.txt的文件,其内容如下:

Let's say I have a file called original.txt with this content:

红色
蓝色

食物

灰色
白色

red
blue
water
food
tree
gray
white

我还有一个名为new.txt的文件,内容如下:

Also I have a file called new.txt with this content:

绿色
黑色
黄色
紫色

green
black
yellow
purple

现在,我想编写一个脚本,用new.txt的内容替换original.txtbluegray之间的行,所以它给了我这个结果:

Now I want to write a script that replaces the lines between blue and gray in original.txt with the contents of new.txt, so it gives me this result:

红色
蓝色
绿色
黑色
黄色
紫色
灰色
白色

red
blue
green
black
yellow
purple
gray
white

我为此目的编写了这段代码(新文件的名称并不总是相同,因此存储在变量中):

I wrote this piece of code for the purpose (the name of the new file is not always the same so it's stored in a variable):

newtext="new.txt"

sed -i "/blue/,/gray/{
    r $newtext
    d
}" original.txt

但是,当我运行它时,却得到了这个废话:

However, when running it I get this nonsense instead:

red
green
black
yellow
purplegreen
black
yellow
purplegreen
black
yellow
purplegreen
black
yellow
purplegreen
black
yellow
purplewhite

我在做什么错了?

推荐答案

这可能对您有用(GNU sed):

This might work for you (GNU sed):

sed '/blue/,/gray/!b;//!d;/blue/r file2' file1

对于bluegray之间的行范围,删除与该范围的第一行或最后一行都不匹配的行,并读取要插入到该范围的最后一行之前的行.

For the range of lines between blue and gray, delete lines that do not match either the first or the last lines in the range and read the lines to insert before the last line of the range.

第一个sed命令/blue/,/grey/!b匹配一系列行,即介于包含blue的行到包含gray的行之间的行. !b部分表示如果这些行不在此范围内,则脱离sed命令,即,不对这些行进行任何进一步的sed处理,只需正常打印即可.

The first sed command /blue/,/grey/!bmatches a range of lines i.e. the lines that range between a line containing blue upto and including a line containing gray. The !b part means if the lines are not in that range then break out of the sed commands i.e. do not do any further sed processing for these lines, just print as normal.

下面的sed命令将只影响bluegray之间的那些行.

The sed commands following will only affect those lines that are in the range between blue and gray.

第二个命令//!d的意思是:删除与范围的开始/结束(即bluegray)不匹配的行. //使用上一个/.../命令中的正则表达式. N.B.删除命令将终止该行的所有进一步sed处理.

The second command //!d means: delete those lines that do not match either the start/end of the range i.e. blue or gray. The // uses the regexp from a previous /.../ command. N.B. a delete command terminates any further sed processing for that line.

以下sed命令仅会影响包含bluegray的行.

The sed commands following will only affect lines that containing either blue or gray.

第三个命令与包含blue的行匹配,并从file2中读取行.

The third command matches a line containing blue and reads in lines from file2.

包含bluegrey的行将自然进行sed处理,并在将下一行读入模式空间之前打印出来,就像不在bluegray之间的行一样.

N.B. the lines containing blue and grey are processed by sed naturally and printed before the next line is read into the pattern space as are the lines not between blue and gray.

替代方法:

sed '/blue/,/gray/!b;//!d;/gray/e cat file2' file1

另一个:

sed -ne '/blue/{p;r file2' -e ':a;n;/gray/!ba};p' file1

这篇关于用存储在sed中的变量的文件内容替换两行之间的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆