本文介绍了为什么不“排序文件 1"?文件 1"工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试对文件进行排序并将排序后的输出本身保存时,就像这样

When I am trying to sort a file and save the sorted output in itself, like this

sort file1 > file1;

file1 的内容被完全删除了,而当我尝试用这样的tee"命令做同样的事情时

the contents of the file1 is getting erased altogether, whereas when i am trying to do the same with 'tee' command like this

sort file1 | tee file1;

它工作正常[ed:工作正常"仅适用于时间幸运的小文件,会导致大文件丢失数据或无用的进程调度],即它正在覆盖排序的输出file1 本身并在标准输出中显示它.

it works fine [ed: "works fine" only for small files with lucky timing, will cause lost data on large ones or with unhelpful process scheduling], i.e it is overwriting the sorted output of file1 in itself and also showing it on standard output.

有人可以解释为什么第一种情况不起作用吗?

Can someone explain why the first case is not working?

推荐答案

它不起作用,因为 '>' 重定向意味着截断,并避免将 sort 的整个输出保留在内存中之前重定向到文件,bash 在运行 sort 之前截断和重定向输出.因此,file1 文件的内容将在 sort 有机会读取它之前被截断.

It doesn't work because '>' redirection implies truncation, and to avoid keeping the whole output of sort in the memory before re-directing to the file, bash truncates and redirects output before running sort. Thus, contents of the file1 file will be truncated before sort will have a chance to read it.

这篇关于为什么不“排序文件 1"?文件 1"工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 10:59