Sort multiple delimited file lexicographically by one column, numerically by another
Clash Royale CLAN TAG#URR8PPP
I wish to sort the TSV file below (called min_ex
) by the first column lexicographically and by the second column numerically.
A X, N 2.2
A, N 5.7
A, A 5.8
A, N 2.1
A, T 0.2
B G, M 2.3
B, L 0.1
B, I 0.2
B, M 9.3
B, C 9.9
I tried to do it with sort -k1,2 -n min_ex
. but it doesn't work as it results in:
A, A 5.8
A, N 2.1
A, N 5.7
A, T 0.2
A X, N 2.2
B, C 9.9
B G, M 2.3
B, I 0.2
B, L 0.1
B, M 9.3
I also think am also pretty sure (through experimentation) that sort
is taking any blank space as the delimiter, but I don't see an option to set the separators.
I'd like to have solutions using either pure AWK or no-sed
at all (preferably both, separately), and I'd like to remain as POSIX compliant as possible.
text-processing awk sort text-formatting columns
|
show 2 more comments
I wish to sort the TSV file below (called min_ex
) by the first column lexicographically and by the second column numerically.
A X, N 2.2
A, N 5.7
A, A 5.8
A, N 2.1
A, T 0.2
B G, M 2.3
B, L 0.1
B, I 0.2
B, M 9.3
B, C 9.9
I tried to do it with sort -k1,2 -n min_ex
. but it doesn't work as it results in:
A, A 5.8
A, N 2.1
A, N 5.7
A, T 0.2
A X, N 2.2
B, C 9.9
B G, M 2.3
B, I 0.2
B, L 0.1
B, M 9.3
I also think am also pretty sure (through experimentation) that sort
is taking any blank space as the delimiter, but I don't see an option to set the separators.
I'd like to have solutions using either pure AWK or no-sed
at all (preferably both, separately), and I'd like to remain as POSIX compliant as possible.
text-processing awk sort text-formatting columns
1
Could you clarify what’s wrong about the sort order used in your example output? It seems to be sorted correctly...
– Stephen Kitt
Jan 7 at 10:09
@StephenKitt Take for instance the first key, the first element isA
. Then, in this group, I'm getting5.8>2.1>5.7
.
– user330157
Jan 7 at 10:18
Oh, OK, so really you want to sort by the first field lexicographically, using tabs, spaces and commas as separators, and by the last field numerically; does that reflect your intentions more accurately? (“First field” in a tab-separated file means that your first field here is “A, A”, “A, N” etc., not just “A”.)
– Stephen Kitt
Jan 7 at 10:22
It does reflect what I wrote more accurately, but not my intentions. I made a mistake. Please give me a few minutes to rethink this. @StephenKitt
– user330157
Jan 7 at 10:24
@StephenKitt I asked the wrong question. Thank you.
– user330157
Jan 7 at 10:31
|
show 2 more comments
I wish to sort the TSV file below (called min_ex
) by the first column lexicographically and by the second column numerically.
A X, N 2.2
A, N 5.7
A, A 5.8
A, N 2.1
A, T 0.2
B G, M 2.3
B, L 0.1
B, I 0.2
B, M 9.3
B, C 9.9
I tried to do it with sort -k1,2 -n min_ex
. but it doesn't work as it results in:
A, A 5.8
A, N 2.1
A, N 5.7
A, T 0.2
A X, N 2.2
B, C 9.9
B G, M 2.3
B, I 0.2
B, L 0.1
B, M 9.3
I also think am also pretty sure (through experimentation) that sort
is taking any blank space as the delimiter, but I don't see an option to set the separators.
I'd like to have solutions using either pure AWK or no-sed
at all (preferably both, separately), and I'd like to remain as POSIX compliant as possible.
text-processing awk sort text-formatting columns
I wish to sort the TSV file below (called min_ex
) by the first column lexicographically and by the second column numerically.
A X, N 2.2
A, N 5.7
A, A 5.8
A, N 2.1
A, T 0.2
B G, M 2.3
B, L 0.1
B, I 0.2
B, M 9.3
B, C 9.9
I tried to do it with sort -k1,2 -n min_ex
. but it doesn't work as it results in:
A, A 5.8
A, N 2.1
A, N 5.7
A, T 0.2
A X, N 2.2
B, C 9.9
B G, M 2.3
B, I 0.2
B, L 0.1
B, M 9.3
I also think am also pretty sure (through experimentation) that sort
is taking any blank space as the delimiter, but I don't see an option to set the separators.
I'd like to have solutions using either pure AWK or no-sed
at all (preferably both, separately), and I'd like to remain as POSIX compliant as possible.
text-processing awk sort text-formatting columns
text-processing awk sort text-formatting columns
edited Jan 7 at 11:39
user330157
asked Jan 7 at 9:49
user330157user330157
233
233
1
Could you clarify what’s wrong about the sort order used in your example output? It seems to be sorted correctly...
– Stephen Kitt
Jan 7 at 10:09
@StephenKitt Take for instance the first key, the first element isA
. Then, in this group, I'm getting5.8>2.1>5.7
.
– user330157
Jan 7 at 10:18
Oh, OK, so really you want to sort by the first field lexicographically, using tabs, spaces and commas as separators, and by the last field numerically; does that reflect your intentions more accurately? (“First field” in a tab-separated file means that your first field here is “A, A”, “A, N” etc., not just “A”.)
– Stephen Kitt
Jan 7 at 10:22
It does reflect what I wrote more accurately, but not my intentions. I made a mistake. Please give me a few minutes to rethink this. @StephenKitt
– user330157
Jan 7 at 10:24
@StephenKitt I asked the wrong question. Thank you.
– user330157
Jan 7 at 10:31
|
show 2 more comments
1
Could you clarify what’s wrong about the sort order used in your example output? It seems to be sorted correctly...
– Stephen Kitt
Jan 7 at 10:09
@StephenKitt Take for instance the first key, the first element isA
. Then, in this group, I'm getting5.8>2.1>5.7
.
– user330157
Jan 7 at 10:18
Oh, OK, so really you want to sort by the first field lexicographically, using tabs, spaces and commas as separators, and by the last field numerically; does that reflect your intentions more accurately? (“First field” in a tab-separated file means that your first field here is “A, A”, “A, N” etc., not just “A”.)
– Stephen Kitt
Jan 7 at 10:22
It does reflect what I wrote more accurately, but not my intentions. I made a mistake. Please give me a few minutes to rethink this. @StephenKitt
– user330157
Jan 7 at 10:24
@StephenKitt I asked the wrong question. Thank you.
– user330157
Jan 7 at 10:31
1
1
Could you clarify what’s wrong about the sort order used in your example output? It seems to be sorted correctly...
– Stephen Kitt
Jan 7 at 10:09
Could you clarify what’s wrong about the sort order used in your example output? It seems to be sorted correctly...
– Stephen Kitt
Jan 7 at 10:09
@StephenKitt Take for instance the first key, the first element is
A
. Then, in this group, I'm getting 5.8>2.1>5.7
.– user330157
Jan 7 at 10:18
@StephenKitt Take for instance the first key, the first element is
A
. Then, in this group, I'm getting 5.8>2.1>5.7
.– user330157
Jan 7 at 10:18
Oh, OK, so really you want to sort by the first field lexicographically, using tabs, spaces and commas as separators, and by the last field numerically; does that reflect your intentions more accurately? (“First field” in a tab-separated file means that your first field here is “A, A”, “A, N” etc., not just “A”.)
– Stephen Kitt
Jan 7 at 10:22
Oh, OK, so really you want to sort by the first field lexicographically, using tabs, spaces and commas as separators, and by the last field numerically; does that reflect your intentions more accurately? (“First field” in a tab-separated file means that your first field here is “A, A”, “A, N” etc., not just “A”.)
– Stephen Kitt
Jan 7 at 10:22
It does reflect what I wrote more accurately, but not my intentions. I made a mistake. Please give me a few minutes to rethink this. @StephenKitt
– user330157
Jan 7 at 10:24
It does reflect what I wrote more accurately, but not my intentions. I made a mistake. Please give me a few minutes to rethink this. @StephenKitt
– user330157
Jan 7 at 10:24
@StephenKitt I asked the wrong question. Thank you.
– user330157
Jan 7 at 10:31
@StephenKitt I asked the wrong question. Thank you.
– user330157
Jan 7 at 10:31
|
show 2 more comments
1 Answer
1
active
oldest
votes
sort -t$'t' -k1,1 -k2,2n
does the trick, and it’s POSIX-compliant apart from the $'t'
part. -t
specifies the field delimiter (instead of blank-to-non-blank transitions, which is the default); the n
suffix can be applied to single field definitions.
Given this exchange of comments, perhaps it is important to mention that I'm on WSL (Ubuntu distro). Is this something I should be adding to the question?
– user330157
Jan 7 at 10:05
@Kusalananda that’s a bit of a brown-paper-bag moment there (we need that as a hat this December)...
– Stephen Kitt
Jan 7 at 10:08
Thank you. Any idea whysort -t$'t' -nk2 min_ex
does not sort by the numerical column?
– user330157
Jan 7 at 10:32
@user330157 in the file you posted on filebin, you had your lines terminated by CR/LF (DOS mode), and 4 spaces instead of Tabs. I've already mentioned that in an answer, but I removed it, since it's not clear what your question is.
– mosvy
Jan 7 at 10:35
@user330157 also-nk2
is not the same as-kn2
.
– mosvy
Jan 7 at 10:45
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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%2f492957%2fsort-multiple-delimited-file-lexicographically-by-one-column-numerically-by-ano%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
sort -t$'t' -k1,1 -k2,2n
does the trick, and it’s POSIX-compliant apart from the $'t'
part. -t
specifies the field delimiter (instead of blank-to-non-blank transitions, which is the default); the n
suffix can be applied to single field definitions.
Given this exchange of comments, perhaps it is important to mention that I'm on WSL (Ubuntu distro). Is this something I should be adding to the question?
– user330157
Jan 7 at 10:05
@Kusalananda that’s a bit of a brown-paper-bag moment there (we need that as a hat this December)...
– Stephen Kitt
Jan 7 at 10:08
Thank you. Any idea whysort -t$'t' -nk2 min_ex
does not sort by the numerical column?
– user330157
Jan 7 at 10:32
@user330157 in the file you posted on filebin, you had your lines terminated by CR/LF (DOS mode), and 4 spaces instead of Tabs. I've already mentioned that in an answer, but I removed it, since it's not clear what your question is.
– mosvy
Jan 7 at 10:35
@user330157 also-nk2
is not the same as-kn2
.
– mosvy
Jan 7 at 10:45
add a comment |
sort -t$'t' -k1,1 -k2,2n
does the trick, and it’s POSIX-compliant apart from the $'t'
part. -t
specifies the field delimiter (instead of blank-to-non-blank transitions, which is the default); the n
suffix can be applied to single field definitions.
Given this exchange of comments, perhaps it is important to mention that I'm on WSL (Ubuntu distro). Is this something I should be adding to the question?
– user330157
Jan 7 at 10:05
@Kusalananda that’s a bit of a brown-paper-bag moment there (we need that as a hat this December)...
– Stephen Kitt
Jan 7 at 10:08
Thank you. Any idea whysort -t$'t' -nk2 min_ex
does not sort by the numerical column?
– user330157
Jan 7 at 10:32
@user330157 in the file you posted on filebin, you had your lines terminated by CR/LF (DOS mode), and 4 spaces instead of Tabs. I've already mentioned that in an answer, but I removed it, since it's not clear what your question is.
– mosvy
Jan 7 at 10:35
@user330157 also-nk2
is not the same as-kn2
.
– mosvy
Jan 7 at 10:45
add a comment |
sort -t$'t' -k1,1 -k2,2n
does the trick, and it’s POSIX-compliant apart from the $'t'
part. -t
specifies the field delimiter (instead of blank-to-non-blank transitions, which is the default); the n
suffix can be applied to single field definitions.
sort -t$'t' -k1,1 -k2,2n
does the trick, and it’s POSIX-compliant apart from the $'t'
part. -t
specifies the field delimiter (instead of blank-to-non-blank transitions, which is the default); the n
suffix can be applied to single field definitions.
edited Jan 7 at 10:06
answered Jan 7 at 10:00
Stephen KittStephen Kitt
168k24378455
168k24378455
Given this exchange of comments, perhaps it is important to mention that I'm on WSL (Ubuntu distro). Is this something I should be adding to the question?
– user330157
Jan 7 at 10:05
@Kusalananda that’s a bit of a brown-paper-bag moment there (we need that as a hat this December)...
– Stephen Kitt
Jan 7 at 10:08
Thank you. Any idea whysort -t$'t' -nk2 min_ex
does not sort by the numerical column?
– user330157
Jan 7 at 10:32
@user330157 in the file you posted on filebin, you had your lines terminated by CR/LF (DOS mode), and 4 spaces instead of Tabs. I've already mentioned that in an answer, but I removed it, since it's not clear what your question is.
– mosvy
Jan 7 at 10:35
@user330157 also-nk2
is not the same as-kn2
.
– mosvy
Jan 7 at 10:45
add a comment |
Given this exchange of comments, perhaps it is important to mention that I'm on WSL (Ubuntu distro). Is this something I should be adding to the question?
– user330157
Jan 7 at 10:05
@Kusalananda that’s a bit of a brown-paper-bag moment there (we need that as a hat this December)...
– Stephen Kitt
Jan 7 at 10:08
Thank you. Any idea whysort -t$'t' -nk2 min_ex
does not sort by the numerical column?
– user330157
Jan 7 at 10:32
@user330157 in the file you posted on filebin, you had your lines terminated by CR/LF (DOS mode), and 4 spaces instead of Tabs. I've already mentioned that in an answer, but I removed it, since it's not clear what your question is.
– mosvy
Jan 7 at 10:35
@user330157 also-nk2
is not the same as-kn2
.
– mosvy
Jan 7 at 10:45
Given this exchange of comments, perhaps it is important to mention that I'm on WSL (Ubuntu distro). Is this something I should be adding to the question?
– user330157
Jan 7 at 10:05
Given this exchange of comments, perhaps it is important to mention that I'm on WSL (Ubuntu distro). Is this something I should be adding to the question?
– user330157
Jan 7 at 10:05
@Kusalananda that’s a bit of a brown-paper-bag moment there (we need that as a hat this December)...
– Stephen Kitt
Jan 7 at 10:08
@Kusalananda that’s a bit of a brown-paper-bag moment there (we need that as a hat this December)...
– Stephen Kitt
Jan 7 at 10:08
Thank you. Any idea why
sort -t$'t' -nk2 min_ex
does not sort by the numerical column?– user330157
Jan 7 at 10:32
Thank you. Any idea why
sort -t$'t' -nk2 min_ex
does not sort by the numerical column?– user330157
Jan 7 at 10:32
@user330157 in the file you posted on filebin, you had your lines terminated by CR/LF (DOS mode), and 4 spaces instead of Tabs. I've already mentioned that in an answer, but I removed it, since it's not clear what your question is.
– mosvy
Jan 7 at 10:35
@user330157 in the file you posted on filebin, you had your lines terminated by CR/LF (DOS mode), and 4 spaces instead of Tabs. I've already mentioned that in an answer, but I removed it, since it's not clear what your question is.
– mosvy
Jan 7 at 10:35
@user330157 also
-nk2
is not the same as -kn2
.– mosvy
Jan 7 at 10:45
@user330157 also
-nk2
is not the same as -kn2
.– mosvy
Jan 7 at 10:45
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f492957%2fsort-multiple-delimited-file-lexicographically-by-one-column-numerically-by-ano%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
1
Could you clarify what’s wrong about the sort order used in your example output? It seems to be sorted correctly...
– Stephen Kitt
Jan 7 at 10:09
@StephenKitt Take for instance the first key, the first element is
A
. Then, in this group, I'm getting5.8>2.1>5.7
.– user330157
Jan 7 at 10:18
Oh, OK, so really you want to sort by the first field lexicographically, using tabs, spaces and commas as separators, and by the last field numerically; does that reflect your intentions more accurately? (“First field” in a tab-separated file means that your first field here is “A, A”, “A, N” etc., not just “A”.)
– Stephen Kitt
Jan 7 at 10:22
It does reflect what I wrote more accurately, but not my intentions. I made a mistake. Please give me a few minutes to rethink this. @StephenKitt
– user330157
Jan 7 at 10:24
@StephenKitt I asked the wrong question. Thank you.
– user330157
Jan 7 at 10:31