gpt4 book ai didi

linux - 如何在 sed 中将命令的结果作为字符串参数输入

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:48:10 25 4
gpt4 key购买 nike

我想在我的 bash 终端上执行如下命令:

sed -i '6i `sed '1!d' input.in`' out

我可以用它在文件的第 6 行插入(替换 -i 选项)sed '%1!d' input.in 命令的结果。我还没有发现任何有用的东西,并且尝试了 `com`、$(com) 和 com | sed -i '6i ' out,其中 com 代表 sed '%1!d' input.in。更改整个命令的语法我没有任何问题,但我希望它在终端使用 sed 时写在一行中。

感谢收听,等待您的答复。

埃德莫顿:

Example Input:

input.in:
into a lake.

out:
Mary was runing around a pond and fell
into a lake.


Mary fell into a what?

Desired Output:
Mary was runing around a pond and fell
into a lake.


Mary fell into a what?
into a lake.

最佳答案

尝试在标准输入上使用 r 而不是 i

sed '%1!d' input.in |
sed -i '6r /dev/stdin' out

如果您的平台不支持/dev/stdin/dev/fd/0,请查看您的sed 是否支持- 表示标准输入...或者,在最坏的情况下,求助于临时文件。

正如评论者已经指出的那样,%1!d 在大多数 sed 方言中似乎不是一个有效的命令,但这在这里基本上不重要。 (如果你打算只打印第一行,也许你的意思是 sed '1!d',虽然 sed 'p;q' 这样做效率更高。)

关于linux - 如何在 sed 中将命令的结果作为字符串参数输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39317465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com