Can you sort one column lexicographically while simultaneously sorting the next column numerically?

Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
I've got two columns of data I need to sort: the first column(A) needs to be sorted lexicographically and for any rows which then contain the same column A string, I need them to be sorted numerically according to what's in the second column(B).
I was thinking 'sort -f' , but that would make a '12' in column B come before a '2'.
Edit: Accidentally typed column in place of row.
sort
add a comment |
up vote
3
down vote
favorite
I've got two columns of data I need to sort: the first column(A) needs to be sorted lexicographically and for any rows which then contain the same column A string, I need them to be sorted numerically according to what's in the second column(B).
I was thinking 'sort -f' , but that would make a '12' in column B come before a '2'.
Edit: Accidentally typed column in place of row.
sort
Possible duplicate of Trying to sort on two fields, second then first
– Alexander
Mar 31 '17 at 6:59
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I've got two columns of data I need to sort: the first column(A) needs to be sorted lexicographically and for any rows which then contain the same column A string, I need them to be sorted numerically according to what's in the second column(B).
I was thinking 'sort -f' , but that would make a '12' in column B come before a '2'.
Edit: Accidentally typed column in place of row.
sort
I've got two columns of data I need to sort: the first column(A) needs to be sorted lexicographically and for any rows which then contain the same column A string, I need them to be sorted numerically according to what's in the second column(B).
I was thinking 'sort -f' , but that would make a '12' in column B come before a '2'.
Edit: Accidentally typed column in place of row.
sort
sort
edited Nov 20 at 20:34
Rui F Ribeiro
38.2k1475125
38.2k1475125
asked Mar 31 '17 at 6:53
Anj
183
183
Possible duplicate of Trying to sort on two fields, second then first
– Alexander
Mar 31 '17 at 6:59
add a comment |
Possible duplicate of Trying to sort on two fields, second then first
– Alexander
Mar 31 '17 at 6:59
Possible duplicate of Trying to sort on two fields, second then first
– Alexander
Mar 31 '17 at 6:59
Possible duplicate of Trying to sort on two fields, second then first
– Alexander
Mar 31 '17 at 6:59
add a comment |
1 Answer
1
active
oldest
votes
up vote
6
down vote
accepted
Yes, using the -k option to define sort keys, and the n option to specify numerical sorts:
$ echo -e "a 13nb 2na 2" | sort -k1,1 -k2,2n
a 2
a 13
b 2
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
Yes, using the -k option to define sort keys, and the n option to specify numerical sorts:
$ echo -e "a 13nb 2na 2" | sort -k1,1 -k2,2n
a 2
a 13
b 2
add a comment |
up vote
6
down vote
accepted
Yes, using the -k option to define sort keys, and the n option to specify numerical sorts:
$ echo -e "a 13nb 2na 2" | sort -k1,1 -k2,2n
a 2
a 13
b 2
add a comment |
up vote
6
down vote
accepted
up vote
6
down vote
accepted
Yes, using the -k option to define sort keys, and the n option to specify numerical sorts:
$ echo -e "a 13nb 2na 2" | sort -k1,1 -k2,2n
a 2
a 13
b 2
Yes, using the -k option to define sort keys, and the n option to specify numerical sorts:
$ echo -e "a 13nb 2na 2" | sort -k1,1 -k2,2n
a 2
a 13
b 2
answered Mar 31 '17 at 7:00
Stephen Kitt
158k23344421
158k23344421
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f354995%2fcan-you-sort-one-column-lexicographically-while-simultaneously-sorting-the-next%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Possible duplicate of Trying to sort on two fields, second then first
– Alexander
Mar 31 '17 at 6:59