Misbehaving sort command in unix

Misbehaving sort command in unix

Post by Vahid Mogh » Wed, 09 Nov 2005 13:02:41


Hi,
I have been trying to sort -u the password file /etc/passwd by its
first field (username) but for some reason sort is giving me a wrong
result. This is what I type:
"sort -t : -k 1,1 /etc/passwd" I do not use -u to see the result of the
sort command.
In the passwd file, I have the first 5 lines copied to the buttom of
the file (duplicate system accounts, root,daemon,bin,sys,adm with
different uid,gid's ) I have the users in the middle.
When I sort -u, I would like to see either the top 5 system users or
the bottom ones but I see one from top, one from the bottom and so on.
There is no pattern to the output, it seems that -k 1,1 does not have
any effect at all.
I would appreciate if someone shed a light on this. I need to enbed
this in a perl script.
Thanks,
Vahid.
 
 
 

Misbehaving sort command in unix

Post by John » Wed, 09 Nov 2005 18:41:53


It is the uid that matters, not the name.




Your problem is that sort is not stable. In other words,
it does not guarantee to preserve the original order when
two items are the same (using whatever comparison is specified).
Some sort commands have a "stable" option.

But in any case, perl has its own sort functions so you do not
need to use the system sort command.

--
John.

 
 
 

Misbehaving sort command in unix

Post by Vahid Mogh » Wed, 09 Nov 2005 23:39:00


Not in my case. I have to sort -u by name then later sort -u by UID.
Do you happen to have the syntax for the above password sort function?
I can not add any module to perl and perl sort must work on all unix
platforms as a default perl install.
Thanks,
 
 
 

Misbehaving sort command in unix

Post by John » Thu, 10 Nov 2005 00:19:03


Ask in one of the comp.lang.perl groups. You can use perl's standard sort.

--
John.
 
 
 

Misbehaving sort command in unix

Post by Michael To » Thu, 10 Nov 2005 02:28:41


So you mean

sort -t : -u -k 1,1 | sort -t : -nu -k 3,3

?

You need these two sorts, or

awk -F: 's[$1]++==0' | sort -t : -nu -k 3,3

--
Michael Tosch @ hp : com
 
 
 

Misbehaving sort command in unix

Post by John » Thu, 10 Nov 2005 07:36:48


If this is a wholly artificial exercise to prototype some
other use of the sort command then fine but the point about
UIDs and usernames is that the UID is what is used to keep
track of permissions and ownership rather than the name.
The superuser account is the one that has the UID 0, not
necessarily the name root.

The relevant part of the OP which got snipped was:

Just to be clear, if you want to duplicate the sytem accounts
then you need to duplicate the UIDs rather than the names (though
it is an odd thing to want to do and there may be better ways).
If, however, you are just doing this to work out how to call
the sort command for some other purpose, then it does not matter.

--
John.
 
 
 

Misbehaving sort command in unix

Post by hymie » Fri, 11 Nov 2005 23:03:13

In our last episode, the evil Dr. Lacto had captured our hero,
"Vahid Moghaddasi" < XXXX@XXXXX.COM >, who said:


If there are different uids and gids, then the lines aren't the same,
are they?

It sounds like "sort -u" doesn't do what you think it will do,
rather than doing it wrong.

hymie! http://www.yqcomputer.com/ ~hymowitz XXXX@XXXXX.COM
===============================================================================