假设源文件为源文件,目标文件为目标文件,下面使用cat命令展示文件内容。
`$cat source_library.txt
// START method library function
{
common code starts...
I am the library which can be used in target files.
you can use me..
}
// END method library function
$ cat target_file.txt (before executing that sed command)
My name is Bikram
// START method library function
{
common code starts...
}
// END method library function
Outside of this method.`
执行以下命令后的输出:
sed -i '/START method library function/,/END method library function/!b;//!d;/START method library function/r source_library.txt' target_file.txt
该命令的输出:
`$cat target_file.txt (after executing that sed command)
My name is Bikram
// START method library function
// START method library function
{
common code starts...
I am the library which can be used in target files.
you can use me..
}
// END method library function
// END method library function
Outside of this method.`
但在Target_file.txt中需要的预期输出为
`My name is Bikram
// START method library function
{
common code starts...
I am the library which can be used in target files.
you can use me..
}
// END method library function
Outside of this method.
`
4条答案
按热度按时间f2uvfpb91#
此
sed
命令应该执行以下工作:请注意,如果包含
START method library function
的行没有对应的END method library function
行,则此sed
命令将无法正常工作。vcudknz32#
使用GNU
sed
neskvpey3#
如果ed可用/可接受。
脚本
script.ed
,随便你怎么命名。现在快跑吧
使用
here document
,类似于。tmp.txt
的文件),其内容来自source_library.txt
+;/}/-d
行之后添加!rm tmp.txt
。stdout
,则移除,p
。Q
更改为w
。jv4diomz4#
cat source_library.txt | sed '/START method library function/,/END method library function/{d;r /dev/stdin}' target_file.txt
正如我们在这个问题中所讨论的,您可以使用
r /dev/stdin
来确保r
正在读取替换部分第一行之后的空流。