庆典从文件重定向输入回同一个文件

庆典从文件重定向输入回同一个文件

问题描述:

基本上,我想作为输入的文本从文件中删除该文件中的一行,并发送回输出到同一个文件。沿着这些线路的东西,如果这使得它更清楚。

Basically I want to take as input text from a file, remove a line from that file, and send the output back to the same file. Something along these lines if that makes it any clearer.

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

不过,我这样做,当我结束了一个空白文件。
有什么想法?

however, when I do this I end up with a blank file. Any thoughts?

您不能这样做。您可以使用临时文件虽然。

You cannot do that. 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.