我需要生成第二个文件来对文件进行排序吗?
我想对一堆文件进行排序。我可以,
sort file.txt > foo.txt
mv foo.txt file.txt
但是我需要第二个文件吗?
(当然,我尝试了 sort file.txt > file.txt
,但最终得到的是一个空文件。)
I want to sort a bunch of files. I can do
sort file.txt > foo.txt
mv foo.txt file.txt
but do I need this second file?
(I tried sort file.txt > file.txt
of course, but then I just ended up with an empty file.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试:
参见 http://ss64.com/bash/sort.html
Try:
See http://ss64.com/bash/sort.html
sort
等经典 Unix 工具的理念包括您可以使用它们构建管道。每个小工具都从STDIN
读取数据并写入STDOUT
。这样,管道中的下一个小工具可以读取第一个小工具的输出作为输入并对其进行操作。所以我想说这是一个错误而不是一个功能。
另请阅读有关 Pipes、重定向和过滤器 的精彩书籍,作者:血沉。
The philosophy of classic Unix tools like
sort
includes that you can build a pipe with them. Every little tool reads fromSTDIN
and writes toSTDOUT
. This way the next little tool down the pipe can read the output of the first as input and act on it.So I'd say that this is a bug and not a feature.
Please also read about Pipes, Redirection, and Filters in the very nice book by ESR.
因为您要写回同一个文件,所以在排序完成加载原始文件之前,您总是会遇到重定向打开输出文件的问题。所以是的,您需要使用单独的文件。
现在,话虽如此,有一些方法可以首先将整个文件缓冲到管道流中,但通常您不想这样做,尽管如果您编写一些东西来做到这一点是可能的。但是您需要在开头和结尾插入特殊工具来进行缓冲。但是,如果您使用 Bash 的
>
重定向,它会过早打开输出文件。Because you're writing back to the same file you'll always end up with a problem of the redirect opening the output file before sort gets done loading the original. So yes, you need to use a separate file.
Now, having said that, there are ways to buffer the whole file into the pipe stream first but generally you wouldn't want to do that, although it is possible if you write something to do it. But you'd be inserting special tools at the beginning and the end to do the buffering. Bash, however, will open the output file too soon if you use it's
>
redirect.是的,您确实需要第二个文件!该命令
将让 bash 在开始执行
sort
之前设置stout
的重定向。这是破坏输入文件的一种特定方式。如果您想对许多文件进行排序,请尝试:
Yes, you do need a second file! The command
would have bash to set up the redirection of
stout
before it starts executingsort
. This is a certain way to clobber your input file.If you want to sort many files try :
如果您要对单个文件中的固定长度记录进行排序,则排序算法可以交换文件内的记录。有一些可用的算法。您的选择将取决于文件随机性属性的数量。一般来说,与其他排序算法相比,快速排序倾向于交换最少数量的记录,并且通常是最先完成的排序。
if you are dealing with sorting fixed length records from a single file, then the sort algorithm can swap records within the file. There are a few available algorithms availabe. Your choice would depend on the amount of the file's randomness properties. Generally, quicksort tends to swap the fewest number of records and is usually the sort that completes first, when compared to othersorting algorithms.