我有一个文件,比方说“大文件”,其中包含以下形式的表格数据,
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/