unix排序的意外行为

wvt8vs2t  于 7个月前  发布在  Unix
关注(0)|答案(2)|浏览(72)

嗨,我正试图使用unix排序功能。我想排序的第三列的文件
这是文件的内容

tcsh 541% cat file
2,2587,769,629,629,629,890,890,890,890,89,801,0,184,24944,8018,42839
1,2622,867,686,686,686,880,880,880,880,88,792,0,246,25551,7977,43721
3,2636,736,700,700,700,1083,880,880,880,88,792,0,260,25439,7984,43758

字符串
当我尝试按第1列排序时,

tcsh 533% sort -t, -nk1 file
1,2622,867,686,686,686,880,880,880,880,88,792,0,246,25551,7977,43721
2,2587,769,629,629,629,890,890,890,890,89,801,0,184,24944,8018,42839
3,2636,736,700,700,700,1083,880,880,880,88,792,0,260,25439,7984,43758


当我尝试按第2列排序时,它起作用了。

tcsh 534% sort -t, -nk1 file | sort -t, -nk2
2,2587,769,629,629,629,890,890,890,890,89,801,0,184,24944,8018,42839
1,2622,867,686,686,686,880,880,880,880,88,792,0,246,25551,7977,43721
3,2636,736,700,700,700,1083,880,880,880,88,792,0,260,25439,7984,43758


但是当我试图按第3列排序时,它不起作用。

tcsh 535% sort -t, -nk1 file | sort -t, -nk3
2,2587,769,629,629,629,890,890,890,890,89,801,0,184,24944,8018,42839
1,2622,867,686,686,686,880,880,880,880,88,792,0,246,25551,7977,43721
3,2636,736,700,700,700,1083,880,880,880,88,792,0,260,25439,7984,43758

tcsh 532% sort -t, -nk3 file
2,2587,769,629,629,629,890,890,890,890,89,801,0,184,24944,8018,42839
1,2622,867,686,686,686,880,880,880,880,88,792,0,246,25551,7977,43721
3,2636,736,700,700,700,1083,880,880,880,88,792,0,260,25439,7984,43758


预期输出为(根据Knitt的评论更新)

tcsh 532% sort -t, -nk3 file
3,2636,736,700,700,700,1083,880,880,880,88,792,0,260,25439,7984,43758
2,2587,769,629,629,629,890,890,890,890,89,801,0,184,24944,8018,42839
1,2622,867,686,686,686,880,880,880,880,88,792,0,246,25551,7977,43721


SORT版本:
542% sort --version sort(GNU coreutils)8.22版权所有(C)2013 Free Software Foundation,Inc.许可证GPLv 3+:GNU GPL version 3或更高版本<gnu.org/licenses/gpl.html>。这是自由软件:您可以自由更改和重新分发它。在法律允许的范围内,没有担保。作者:Mike Haertel和Paul Eggert

13z8s7eq

13z8s7eq1#

如@

setenv LC_ALL C

字符串
获取预期输出

tcsh 545% sort -t, -nk3 file
3,2636,736,700,700,700,1083,880,880,880,88,792,0,260,25439,7984,43758
2,2587,769,629,629,629,890,890,890,890,89,801,0,184,24944,8018,42839
1,2622,867,686,686,686,880,880,880,880,88,792,0,246,25551,7977,43721


获取更多关于LC_ALL here的信息

q8l4jmvw

q8l4jmvw2#

以下选项-k的规范解决了这个问题:

sort -t, -nk3,3 file

字符串

相关问题