在终端中运行排序命令

在终端中运行排序命令

我有这个文件:

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
nickson:x:999:504:instructor:/home/SI/nickson:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
sabina:x:500:500:Joen Doe:/home/joendoe:/bin/bash
student:x:501:501::/home/student:/bin/bash
svi:x:502:500:foo fooo:/home/svi:/bin/bash
tslavova:x:503:500:foo ofoofofo:/home/das:/bin/bash
jbabukov:x:504:500:ofofofo f of of o:/home/adas:/bin/bash
griza:x:505:500:asdasdas:/home/dasd:/bin/bash
moni:x:506:500:adsdasd:/home/dasi:/bin/bash
victoria:x:507:500:asdasd:/home/dadfeia:/bin/bash
kiril:x:508:500:asdasd:/home/kdasdail:/bin/bash

我想要按第三个字段的字典顺序对其进行排序。为此,我运行

sort -t ':' -k3 file.txt

一切看起来都很好,除了最后两行

nickson:x:999:504:instructor:/home/SI/nickson:/bin/bash
nobody:x:99:99:Nobody:/:/sbin/nologin

我认为应该将其反转。有人能解释一下为什么会发生这种情况吗?

答案1

你没有排序仅有的按第三个字段排序。如果您未同时指定开始和结束字段,则排序将使用开始字段的开头到行尾进行。从man sort

KEYDEF  is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where
F is a field number and C a character position in the field;  both  are
origin 1, and the stop position defaults to the line's end. 

因此:...也包括在排序中。

-k3,3如果只需要根据第三个字段排序,请使用:

% sort -t: -k3,3 foo | grep '^n' 
ntp:x:38:38::/etc/ntp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
nickson:x:999:504:instructor:/home/SI/nickson:/bin/bash

使用 GNU sort 你可以验证发生了什么--debug

$ sort -t: -k3,3 foo --debug | tail    
sort: using ‘en_GB.UTF-8’ sorting rules
______________________________________________
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
          __
_________________________________________________
nobody:x:99:99:Nobody:/:/sbin/nologin
         __
_____________________________________
nickson:x:999:504:instructor:/home/SI/nickson:/bin/bash
          ___
_______________________________________________________
$ sort -t: -k3 foo --debug | tail 
sort: using ‘en_GB.UTF-8’ sorting rules
______________________________________________
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
          _______________________________________
_________________________________________________
nickson:x:999:504:instructor:/home/SI/nickson:/bin/bash
          _____________________________________________
_______________________________________________________
nobody:x:99:99:Nobody:/:/sbin/nologin
         ____________________________
_____________________________________

每行正下方的虚线显示比较的部分。

相关内容