Sed确实有一个内置的读文件命令.你想要的命令看起来像这样:
$ sed -e '/INSERT_HERE1/ {
r FILE1
d }' -e '/INSERT_HERE2/ {
r FILE2
d }' < file
Run Code Online (Sandbox Code Playgroud)
这会输出
foo
this is file1
bar
this is file2
baz
Run Code Online (Sandbox Code Playgroud)
r命令读取文件,d命令删除带有INSERT_HERE标记的行.因为sed命令必须从它们自己的行开始,所以你需要使用大括号命令和多行输入的花括号,并且根据你的shell,你可能需要\在行的末尾以避免过早执行.如果这是你会sed -f经常使用的东西,你可以把命令放在一个文件中并用来运行它.
如果你熟悉 Perl,你可以这样做:
$ cat FILE1
this is file1
$ cat FILE2
this is file2
$ cat file
foo
INSERT_HERE1
bar
INSERT_HERE2
baz
$ perl -ne 's/^INSERT_HERE(\d+)\s+$/`cat FILE$1`/e;print' file
foo
this is file1
bar
this is file2
baz
$
Run Code Online (Sandbox Code Playgroud)