如何就地对文件进行排序 [英] How to sort a file in-place

查看:30
本文介绍了如何就地对文件进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们使用sort file命令时,该文件以一种排序的方式显示其内容,如果我不想获得任何类型的输出而是一个排序的文件怎么办?

When we use sort file command, the file shows its contents in a sorted way what if I don't want to get any-kind of output but a sorted file?

推荐答案

可以使用文件重定向来重定向排序后的输出:

You can use file redirection to redirected the sorted output:

sort input-file > output_file

或者你可以使用-o--output=FILE sort 选项来表示相同的输入和输出文件:

Or you can use the -o, --output=FILE option of sort to indicate the same input and output file:

sort -o file file

不重复文件名(使用 bash 大括号扩展)

Without repeating the filename (with bash brace expansion)

sort -o file{,}

⚠️ 注意: 一个常见的错误是尝试将输出重定向到同一个输入文件(例如 sort file > file).这不起作用,因为 shell 正在进行重定向(不是 sort(1) 程序)并且输入文件(也作为输出)将在提供 sort(1)计划阅读它的机会.

⚠️ Note: A common mistake is to try to redirect the output to the same input file (e.g. sort file > file). This does not work as the shell is making the redirections (not the sort(1) program) and the input file (as being the output also) will be erased just before giving the sort(1) program the opportunity of reading it.

这篇关于如何就地对文件进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆