0

I have this file:

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

and I want to sort it lexicographically by the third field. In order to do it I run

sort -t ':' -k3 file.txt

and everything looks good except the last two lines where I have

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

which I think should be in reversed positions. Can some explain to me why this is happening?

0

1 Answer 1

5

You're not sorting only by the third field. If you don't specify both start and end fields, sorting is done using the start of start field to the end of the line. From 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. 

So the :... is also included in the sort.

Use -k3,3 if you need to sort only on the third field:

% 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

With GNU sort you can verify what's happening with --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
         ____________________________
_____________________________________

The dashes immediately below each line shows the compared section.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .