linux - 如何对文本文件进行数字排序,然后将结果存储在同一个文本文件中?

标签 linux

我试过 sort -n test.text > test.txt。但是,这给我留下了一个空文本文件。这是怎么回事,我该怎么做才能解决这个问题?

最佳答案

排序不会就地排序文件。它输出一个排序的副本。

你需要 sort -n -k 4 out.txt > sorted-out.txt

编辑:要获得您想要的顺序,您必须使用倒序读取的数字对文件进行排序。这样做:

cut -d' ' -f4 out.txt | rev | paste - out.txt | sort -k1 -n | cut -f2- > sorted-out.txt

更多学习-

sort -nk4 file

-n for numerical sort

-k for providing key

or add -r option for reverse sorting

sort -nrk4 file

关于linux - 如何对文本文件进行数字排序,然后将结果存储在同一个文本文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29829787/

相关文章:

linux - 打字警告

linux - 使用 awk 或 sed 从多个文件中消除重复行

php - 向类(class)添加 moodle (3.1.7) 事件时出现黑屏

windows - Linux 服务器中的 TCP 窗口缩放 - 可能的副作用

C 代码到 HEX 文件

c - 在内存不足导致段错误之前抢先挂起应用程序

java - 使用 Linux more 命令时 Runtime.getRuntime().exec() 挂起

c - 在 C linux 程序中取消链接多个文件

c - 如何通过 xcb RandR 扩展获得 RandR 输出的分辨率?

linux - 使用 while/for 循环和 'find' 命令来复制文件和目录