bash 如何通过在特定字符处拆分来对终端 Linux 中的文本文件的内容进行排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16524353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:25:08  来源:igfitidea点击:

How to sort content of a text file in Terminal Linux by splitting at a specific char?

linuxbashsortingterminalcommand

提问by Payerl

I have an assignment in school to sort a files content in a specific order. I had to do it with Windows batch-files first and now I have to do the same in Linux. The file looks more or less like this the whole way through:

我在学校有一项作业,要按特定顺序对文件内容进行排序。我必须先用 Windows 批处理文件来做,现在我必须在 Linux 上做同样的事情。该文件在整个过程中或多或少看起来像这样:

John Doe : Crocodiles : 1035

John Doe : Crocodiles : 1035

In windows I solved the problem by this:

在 Windows 中,我通过以下方式解决了这个问题:

sort /r /+39 file.txt

The rows in the file are supposed to get sorted by the number of points (which is the number to the right) in decreasing order. Also the second part of the assignment is to sort the rows by the center column.

文件中的行应该按点数(右侧的数字)降序排序。作业的第二部分也是按中心列对行进行排序。

How can I get the same result(s) in Linux? I have tried a couple of different variations of the sort command in Linux too but so far without success.

如何在 Linux 中获得相同的结果?我也在 Linux 中尝试了 sort 命令的几种不同变体,但到目前为止都没有成功。

回答by parkydr

I'd do it with:

我会这样做:

sort -nr -t: -k3

-nr- numbers reverse order -t:- key separator colon -k3- third field

-nr- 数字逆序 -t:- 键分隔符冒号 -k3- 第三个字段

回答by dogbane

The Linux equivalent of your Windows command, sort /r /+39 file, is:

相当于您的 Windows 命令的 Linuxsort /r /+39 file是:

sort -r -k +39 file