Solved

# unexpected sort behaviour

Posted on 2011-05-09
Medium Priority
460 Views
Hi

Why is sort doing this:

e\$ sort -k1 -nk2 <<END
X  1
X  3
Y  2
X  4
END

X  1
Y  2
X  3
X  4

I wanted to sort by the first column and then by the second column to get

X 1
X 3
X 4
Y 2

Can i do this in one sort command or do i have to pipe one sort into another

thanks
0
Question by:andieje
• 7
• 4
• 4
• +1

LVL 38

Expert Comment

ID: 35722419
sort -k1,2 <END
X  1
X  3
Y  2
X  4
END
0

LVL 40

Expert Comment

ID: 35722463
did you try

sort -k1,k2 <<END
X  1
X  3
Y  2
X  4
END
0

LVL 58

Expert Comment

ID: 35722464
sort -t" " -k1,1 -nk2,2 <<END
0

LVL 38

Expert Comment

ID: 35722466
Woo, typo
sort -k1,2 <<END
X  1
X  3
Y  2
X  4
END
0

LVL 40

Expert Comment

ID: 35722479
sorry, correction

sort -k 1,2 <<END
X  1
X  3
Y  2
X  4
END
0

Author Comment

ID: 35722510
why dont i need 2 -k flags if i am sorting on 2 fields
0

Author Comment

ID: 35722529
i obviously don't understand the comma notation
0

Author Comment

ID: 35722542
also why don't i need to use an 'n' to tell sort to sort the second column numerically
0

LVL 58

Expert Comment

ID: 35722545
Does it work for you? Try it and let us know.
0

LVL 40

Expert Comment

ID: 35722576
The man page show the format of -k option:

http://linux.die.net/man/1/sort

Why you don't need -n , I think it sorts numerically by default
0

LVL 58

Expert Comment

ID: 35722583
You need two -k in this case because you want to sort differently on two different columns i.e. first column is tgo be sorted alphabetically and second numerically. Comma notation is

-k, --key=POS1[,POS2]
start a key at POS1 (origin 1), end it at POS2 (default end of line)

If you were to sort both fields alphabetically, you don't need the comma as by default the field definition would extend from the first field to end of the line.
0

Author Comment

ID: 35722584
that doesnt work:

andrea@oscar:~/cattle\$ sort -k1.4,2 <<END
X  1123123123
X  3
Y  2
X  4
END

X  1123123123
X  3
X  4
Y  2

0

Author Comment

ID: 35722593
andrea@oscar:~/cattle\$ sort -k1,2 <<END
X  1123123123
X  3
Y  2
X  4
END

X  1123123123
X  3
X  4
Y  2
0

LVL 40

Expert Comment

ID: 35722602
what is the . (dot) in your command?
0

LVL 58

Accepted Solution

amit_g earned 1000 total points
ID: 35722641
\$ sort -t" " -k1,1 -k2,2n <<END
> X 12345
> X 567
> X 213
> Y 34
> Y 26
> X 45
> Y 3
> X 12
> END
X 12
X 45
X 213
X 567
X 12345
Y 3
Y 26
Y 34
0

LVL 38

Assisted Solution

wesly_chen earned 1000 total points
ID: 35722716
sort -k1,1 -nk2,2 <<END
X  12345
X  3
X  4
Y  2
END
0

Author Comment

ID: 35722742
thank you. i had misunderstood the comma notation
0

Author Closing Comment

ID: 35723945
thanks
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that senâ€¦
This article will show you step-by-step instructions to build your own NTP CentOS server.  The network diagram shows the best practice to setup the NTP server farm for redundancy. Â This article also serves as your NTP server documentation.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
###### Suggested Courses
Course of the Month16 days, 22 hours left to enroll