我需要生成第二个文件来对文件进行排序吗?

发布于 2025-01-02 11:38:16 字数 191 浏览 0 评论 0原文

我想对一堆文件进行排序。我可以,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

遗失的美好 2025-01-09 11:38:16

尝试:

sort -o file.txt file.txt

参见 http://ss64.com/bash/sort.html

`-o OUTPUT-FILE'
     Write output to OUTPUT-FILE instead of standard output.  If
     OUTPUT-FILE is one of the input files, `sort' copies it to a
     temporary file before sorting and writing the output to
     OUTPUT-FILE.

Try:

sort -o file.txt file.txt

See http://ss64.com/bash/sort.html

`-o OUTPUT-FILE'
     Write output to OUTPUT-FILE instead of standard output.  If
     OUTPUT-FILE is one of the input files, `sort' copies it to a
     temporary file before sorting and writing the output to
     OUTPUT-FILE.
十六岁半 2025-01-09 11:38:16

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 from STDIN and writes to STDOUT. 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.

菊凝晚露 2025-01-09 11:38:16

因为您要写回同一个文件,所以在排序完成加载原始文件之前,您总是会遇到重定向打开输出文件的问题。所以是的,您需要使用单独的文件。

现在,话虽如此,有一些方法可以首先将整个文件缓冲到管道流中,但通常您不想这样做,尽管如果您编写一些东西来做到这一点是可能的。但是您需要在开头和结尾插入特殊工具来进行缓冲。但是,如果您使用 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.

耳根太软 2025-01-09 11:38:16

是的,您确实需要第二个文件!该命令

sort file.txt > file.txt

将让 bash 在开始执行 sort 之前设置 stout 的重定向。这是破坏输入文件的一种特定方式。

如果您想对许多文件进行排序,请尝试:

cat *.txt | sort > result.txt

Yes, you do need a second file! The command

sort file.txt > file.txt

would have bash to set up the redirection of stout before it starts executing sort. This is a certain way to clobber your input file.

If you want to sort many files try :

cat *.txt | sort > result.txt
不寐倦长更 2025-01-09 11:38:16

如果您要对单个文件中的固定长度记录进行排序,则排序算法可以交换文件内的记录。有一些可用的算法。您的选择将取决于文件随机性属性的数量。一般来说,与其他排序算法相比,快速排序倾向于交换最少数量的记录,并且通常是最先完成的排序。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文