Text 将分组的文本行复制并粘贴到另一个文件中的优雅方式

Text 将分组的文本行复制并粘贴到另一个文件中的优雅方式,text,command-line,sed,copy-paste,Text,Command Line,Sed,Copy Paste,假设我有两个文件。如何执行以下操作,即将标记零件从一个文件复制到另一个文件的特定位置?一些sed命令会做这项工作吗?最实用的方法是什么 文件#1: This paragraph does not belong to the poem. { begin passage #1 } When from a place he run away, He never at the place did stay; And while he run, as I am told, He never stood

假设我有两个文件。如何执行以下操作,即将标记零件从一个文件复制到另一个文件的特定位置?一些
sed
命令会做这项工作吗?最实用的方法是什么

文件#1:

This paragraph does not belong to the poem.

{ begin passage #1 }

When from a place he run away,
He never at the place did stay;
And while he run, as I am told,
He never stood still for young or old.

He often squeaked, and sometimes violent,
And when he squeaked he never was silent.
Though never instructed by a cat,
He knew a mouse was not a rat.

{ end passage #1 }

This as well does not.
There was a little guinea pig,
Who being little, was not big;
He always walked upon his feet,
And never fasted when he eat.

{ input passage #1 file #1 }

One day, as I am certified,
He took a whim, and fairly died;
And as I am told by men of sense,
He never has been living since.
文件#2:

This paragraph does not belong to the poem.

{ begin passage #1 }

When from a place he run away,
He never at the place did stay;
And while he run, as I am told,
He never stood still for young or old.

He often squeaked, and sometimes violent,
And when he squeaked he never was silent.
Though never instructed by a cat,
He knew a mouse was not a rat.

{ end passage #1 }

This as well does not.
There was a little guinea pig,
Who being little, was not big;
He always walked upon his feet,
And never fasted when he eat.

{ input passage #1 file #1 }

One day, as I am certified,
He took a whim, and fairly died;
And as I am told by men of sense,
He never has been living since.
我想把文件#1中的段落插入文件#2中的给定标记处。感谢您的帮助和想法

当你问“最实用的方法是什么”时,我的回答是混合使用
sed
awk
使用shell脚本将整个过程粘合在一起

仅使用
sed
解决此问题可能是可能的,但不值得花时间来解决

几乎可以肯定会有进一步的优化,但我尝试编写您可以理解的代码,而不是一行代码;-)

您可以更改
#/bin/ksh
#/bin/bash
如果需要

您可以对输出进行后期筛选以消除重复的空行,但不清楚您的最终需求是什么

IHTH

这可能适合您(GNU-sed):


过滤文件1中的行并将其插入文件2中。第一个sed命令中的stdout将成为第二个sed命令的stdin文件。在第二次sed调用中需要两个sed命令,因为
r
命令必须以换行符结束(或一个新的
-e
sed命令)。第二个sed命令删除第一个文件中插入行的行。

您是否希望为
通道1(文件1)
设置不同的值,还是可以接受硬编码解决方案?我无法想出该sed/awk脚本。。。很好,又学到了新东西!:)少就是多!很好的替代品,对一些人来说,是金色的1-liner;-)祝大家好运。
sed -n '/{ begin/,/{ end/!b;//!p' file1 | sed -e '/{ input/r /dev/stdin' -e '//d' file2