bash - 在不使用临时文件的情况下对文件进行排序

标签 bash shell unix sorting

我想在 unix shell 中对文件进行排序。我可以将我的结果重定向到我的输入文件吗?

例如如果我的输入文件是 foo 那么我可以使用

    sort foo > foo   

或者我应该使用:

    sort -o foo foo

以上两者有什么区别?

谢谢,

最佳答案

使用

sort -o foo foo

来自 man 页面:

-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.

sort foo > foo 表示将输出写入标准输出,标准输出被重定向到输出文件。在重定向之前,> 将截断/覆盖输出文件(如果存在)。由于输入和输出文件相同,您将丢失输入文件信息。

关于bash - 在不使用临时文件的情况下对文件进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7720363/

相关文章:

c++ - 如何将程序变成守护程序

shell - WAR文件部署中的Grails Shell

linux - 'none' 命令中的 'mount' 是什么意思?

android - 在 Android 4.2.2 上安装 libhoudini(ARM 仿真)。模拟器

bash - 检查curl增量(--continue-at)下载是否成功

linux - Bash 脚本中的 While 循环?

用于获取和解码 kubernetes secret 的 Bash 函数

使用 shell 脚本的 Java 代码格式化

linux - rsync,忽略根同步目录隐藏文件

linux - Unix Shell 在里面创建多个目录和随机文件(空)