我使用-k选项测试了sort命令,但是我发现从POS1到POS2的sort -k POS1 [,POS2]无效
$ cat words.txt
1 1
2 2
2 1
$ sort -k 1 words.txt
1 1
2 1
2 2
$ sort -k 1,1 words.txt
1 1
2 1
2 2
我认为“ sort -k 1,1 words.txt”应该返回结果为“ cat words.txt”,因为sort仅使用第一列,如果第一列相同,则默认打印顺序。
答案 0 :(得分:1)
假设您使用的是GNU sort
,则需要指定选项-s
进行稳定排序。
$ sort --version
sort (GNU coreutils) 8.28
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Mike Haertel and Paul Eggert.
$ sort -k 1,1 words.txt
1 1
2 1
2 2
$ sort -k 1,1 -s words.txt
1 1
2 2
2 1
有关详细信息,请参见sort
实用程序的手册页。