且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在命令中使用文件并将输出重定向到同一文件而不截断它?

更新时间:2023-11-23 11:27:16

你不能这样做,因为bash首先处理重定向,然后执行命令。所以当grep查看file_name时,它已经是空的。你可以使用一个临时文件。

You cannot do that because bash processes the redirections first, then executes the command. So by the time grep looks at file_name, it is already empty. You can use a temporary file though.

grep -v 'seg[0-9]\{1,\}\.[0-9]\{1\}' file_name > tmpfile
cat tmpfile > file_name

这样,考虑使用 mktemp 来创建 tmpfile 但请注意它不是POSIX。

like that, consider using mktemp to create the tmpfile but note that it's not POSIX.