使用 sed 从一个文件移动动态字符串并附加到第二个文件

using sed to move dynamic string from one file and appending to second file

我正在尝试从一个文件中移动一个动态字符串,然后使用 sed 将其附加到第二个文件中。

到目前为止我尝试过的:

sed -i -e '/To300/{w /home/test/test1.txt' -e 'd}' /home/test/test2.txt

其中 XXXXTo300 是我要移动的字符串。它将字符串移动到第二个文件,但是当移动下一个字符串时,它会覆盖第二个文件中的现有字符串。 谢谢

一般来说,这不会起作用,因为 sed 每次您使用该命令时都会覆盖文件。但是,如果您使用 GNU sed,则可以通过写入 stdout 并重定向来解决此限制:

sed -i -e '/To300/{w /dev/stdout' -e ';d }' oldfile >> newfile