linux - 如何使用内置的 "sort"程序同时按两个字段(一个数字,一个字符串)排序?

标签 linux shell unix sorting

我有一个文件,比方说“大文件”,其中包含以下形式的表格数据,

a1 b2 a3 1
b1 a2 c3 0
...等等。

我想在我的 Linux 机器上使用内置的“排序”程序,所以按第四个字段(数字)然后同时按第一个字段对这个文件进行排序。我浏览了几次手册页,我所能想到的就是,

sort -n -k4,4 -k1,1 bigfile

有没有办法让“排序”按照我的意愿进行,或者我必须编写自己的自定义程序?

谢谢。

最佳答案

来自手册页:

POS is F[.C][OPTS], where F is the field number and C the character position in the field; both are origin 1. If neither -t nor -b is in effect, characters in a field are counted from the beginning of the preceding whitespace. OPTS is one or more single-letter ordering options, which override global ordering options for that key. If no key is given, use the entire line as the key.

sort -k4,4n -k1,1 bigfile 应该这样做。

另一个选项是 sort -k1,1 bigfile | sort --stable -n -k4,4 稳定排序意味着第 4 个字段上的关系由初始位置解析,该位置由排序的第一遍设置为第一个字段。

关于linux - 如何使用内置的 "sort"程序同时按两个字段(一个数字,一个字符串)排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5206033/

相关文章:

shell - 覆盖匹配的行

mysql - 为什么我尝试创建 MySQL 数据库时权限被拒绝?

mysql - 使用shell脚本访问mysql

linux - 增加 Amazon EBS 卷的大小 : "Unknown label"

Python 多处理 : DB connection not available

python - 为什么 Theano 在 Windows 上比在 Linux 上慢(很多)?

linux - 删除 shell 中以某个字符开头的行,但保留负值

unix - tomcat无法启动

bash - 如何使用 bc 计算数字的对数?

linux - shell 脚本 : Using grep and tr in the shebang