bash 使用 sed 将文本附加到文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22289522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:43:21  来源:igfitidea点击:

Append text to file using sed

bashshelltextsededit

提问by Geofferey

How can I write text to a file using sed? More specifically I would it add null variables to my blank text file that was created using touch. The syntax of sed is very confusing to me.

如何使用 sed 将文本写入文件?更具体地说,我会将空变量添加到我使用触摸创建的空白文本文件中。sed 的语法让我很困惑。

回答by Ignacio Vazquez-Abrams

If you're just appending text to the end of the file then you wouldn't use sed in the first place.

如果您只是将文本附加到文件的末尾,那么您首先不会使用 sed。

echo "some text" >> somefile.txt

回答by user4896506

Use $ a.

使用$ a.

sed -i "$ a some text" somefile.txt

回答by DaveParillo

Adding a late post because while the accepted answer is the simplest solution to this problem, the question actually highlights a couple of common situations:

添加一个迟到的帖子,因为虽然接受的答案是这个问题的最简单的解决方案,但这个问题实际上突出了几种常见情况:

  1. Need to edit a file via sudo
  2. Trying to use sedto modify an empty file.
  1. 需要通过 sudo 编辑文件
  2. 试图用来sed修改一个空文件。

In short, you can't touch, then edit a file with sed. Sed doesn't work on empty files, but occasionally you need to do the equivalent of

简而言之,您不能touch,然后使用sed. sed 对空文件不起作用,但偶尔你需要做相当于

sudo echo "some text" >> somefile.txt

sudo doesn't like the redirect, but there are workarounds:

sudo 不喜欢重定向,但有解决方法:

echo "some text" | sudo tee somefile.txt

or if pipes are not an option either, or you simply mustuse sed, or you just like complexity:

或者如果管道也不是一种选择,或者您必须使用 sed,或者您只是喜欢复杂性:

dd if=/dev/zero count=1 bs=1 of=somefile.txt
sed -i '$ a some text' somefile.txt
sed -i '1 d' somefile

On some systems,. you might have to use sed -i -e '$ a ...

在某些系统上,。你可能不得不使用sed -i -e '$ a ...

回答by ghoti

This sounds like an XY Problemto me. You might get better or more useful answers if you ask about what problem you're trying to solve, rather than asking for help implementing a particular solution.

这对我来说听起来像是XY 问题。如果您询问您正在尝试解决什么问题,而不是寻求帮助实施特定解决方案,您可能会得到更好或更有用的答案。

But if you absolutely must do this in sed, you can use sed's rcommand to read a file. For example:

但是,如果您绝对必须在 sed 中执行此操作,则可以使用 sed 的r命令来读取文件。例如:

[ghoti@pc ~/tmp]$ cat one
RED
BLUE
[ghoti@pc ~/tmp]$ cat two
green
yellow
[ghoti@pc ~/tmp]$ echo ">> start"; sed '$r two' one; echo ">> end"
>> start
RED
BLUE
green
yellow
>> end
[ghoti@pc ~/tmp]$ 

The sed command $r twocauses sed to slurp in the file named "two" after processing the last line of input (which in this case is from the file "one").

sed 命令$r two在处理完最后一行输入(在本例中来自文件“one”)后导致 sed 在名为“two”的文件中发出声音。

Note that this merges one file into the stream of another file, but sed's "standard output" (stdout) is what contains the full/combined stream. You can redirect that output to a file per Ignacio's answer if that's what you want.

请注意,这会将一个文件合并到另一个文件的流中,但 sed 的“标准输出” ( stdout) 是包含完整/组合流的内容。如果这是您想要的,您可以根据 Ignacio 的回答将该输出重定向到一个文件。