中断 sed 是否会损坏目标文件?

中断 sed 是否会损坏目标文件?

如果我打断 sed(流编辑器)有没有机会腐败目标文件?

例子:如果我运行该命令sed -i -- 's/foo/bar/g' file.txt,并在完成之前将其终止,该字符串是否有可能foo被目标文件以外的其他文本替换,bar甚至会损坏目标文件?

我会做什么预计是如果我在文件处理程序之前杀死,那么文件中就有可能存在未完成的替换。然而,我有一种感觉,那sed就是强壮的对于此类事件,通过在修改之前使用某种中间文件目标。但是,我在文档中找不到详细信息。

答案1

从 sed 手册...

https://www.gnu.org/software/sed/manual/sed.html

当到达文件末尾时,临时文件将重命名为输出文件的原始名称。

-i[SUFFIX]
--in-place[=SUFFIX]
This option specifies that files are to be edited in-place. GNU sed does this by creating a temporary file and sending output to this file rather than to the standard output.1.
This option implies -s.

When the end of the file is reached, the temporary file is renamed to the output file's original name. The extension, if supplied, is used to modify the name of the old file before renaming the temporary file, thereby making a backup copy2).

This rule is followed: if the extension doesn't contain a *, then it is appended to the end of the current filename as a suffix; if the extension does contain one or more * characters, then each asterisk is replaced with the current filename. This allows you to add a prefix to the backup file, instead of (or in addition to) a suffix, or even to place backup copies of the original files into another directory (provided the directory already exists).

If no extension is supplied, the original file is overwritten without making a backup. 

相关内容