Operating System - HP-UX
1823471 Members
2400 Online
109660 Solutions
New Discussion юеВ

What can this sort command do?

 
SOLVED
Go to solution
Rashid Ali
Frequent Advisor

What can this sort command do?

I saw a book cited an example as shown below,
#sort -t: +2n -3 /etc/passwd

Can someone tell me how to interpret this?

Thanks in advance!
6 REPLIES 6
Stefan Farrelly
Honored Contributor

Re: What can this sort command do?


This sort command basically displays the passwd file sorted by UID (the 3rd field).

The -t: command means use the colan as a field separator (instead of a space or tab), then the normal sort notation of +X -Y means to sort by fields X to Y, in this case 2 to 3 (the UID is the 3rd field in the passwd file after userid and password), so sort after 2 and until the 3rd field. The n on +2n means sort numerically (as opposed to alphabetically) as the UID field are numbers.

See man on sort for more detailed info.
Im from Palmerston North, New Zealand, but somehow ended up in London...
federico_3
Honored Contributor
Solution

Re: What can this sort command do?

this command use with the t option as field separator ": ". The option +2n causes sort to skip the first 2 colomns and start to sort at the third field of each /etc/passwd line ( ID number) . -3 says that in case of equal third field, to put the first fields ( unix user ) in the right order.

federico
Rashid Ali
Frequent Advisor

Re: What can this sort command do?

What's the difference between the previous command and "sort -t: +2n /etc/passwd" ?
I did a test and it seems getting the same result.
Stefan Farrelly
Honored Contributor

Re: What can this sort command do?


there is no difference between +2n -3 or just using +2n. Just using +2n means do the sort calculation until the next field separator, which in this case is a colan so the result is the same. However, good practice is to use the full sort command format, field +X to -Y, if you use the sort command to sort other files you may need the -Y so good practicse is to use it.
Im from Palmerston North, New Zealand, but somehow ended up in London...
Rashid Ali
Frequent Advisor

Re: What can this sort command do?

So sort -t: +3n -4 /etc/passwd will sort the file by Group id,right? But if I want to sort firstly by Group ID (primary key) and then User id (secondary key), should I use
sort -t: -k 4n,4 -k 3n,3 /etc/passwd ?
federico_3
Honored Contributor

Re: What can this sort command do?

Yes, it's ok.


federico