小编coh*_*rte的帖子

sed:用标准输入替换多行块

给出的input_file

1
2
START
foo
END
3
Run Code Online (Sandbox Code Playgroud)

目标START是将 include ...块内的内容替换END为来自标准输入的多行内容:

1
2
hello 
world
3
Run Code Online (Sandbox Code Playgroud)

我尝试过的:

sed '/^START$/,/^END$/d;r /dev/stdin' input_file <<EOF 
hello
world
EOF
Run Code Online (Sandbox Code Playgroud)

不幸的是导致

1
hello
world
2
3
Run Code Online (Sandbox Code Playgroud)

我猜想是在第一行之后r /dev/stdin按顺序调用的,并且只是在第一行之后附加。 /^START$/,/^END$/d;

第二次尝试:

sed '/^START$/,/^END$/{d;r /dev/stdin
}' input_file <<EOF     
hello
world
EOF
Run Code Online (Sandbox Code Playgroud)

印刷

1
2
3
Run Code Online (Sandbox Code Playgroud)

为什么上述命令(尤其是最后一个命令)会打印错误的结果?我该如何调整这些?

sed text-processing stdin here-document

4
推荐指数
1
解决办法
1631
查看次数

标签 统计

here-document ×1

sed ×1

stdin ×1

text-processing ×1