Sort multiple delimited file lexicographically by one column, numerically by another












4














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.










share|improve this question









New contributor




user330157 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    Could you clarify what’s wrong about the sort order used in your example output? It seems to be sorted correctly...
    – Stephen Kitt
    yesterday










  • @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
    yesterday










  • 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
    yesterday










  • 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
    yesterday










  • @StephenKitt I asked the wrong question. Thank you.
    – user330157
    yesterday
















4














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.










share|improve this question









New contributor




user330157 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    Could you clarify what’s wrong about the sort order used in your example output? It seems to be sorted correctly...
    – Stephen Kitt
    yesterday










  • @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
    yesterday










  • 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
    yesterday










  • 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
    yesterday










  • @StephenKitt I asked the wrong question. Thank you.
    – user330157
    yesterday














4












4








4


1





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.










share|improve this question









New contributor




user330157 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question









New contributor




user330157 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




user330157 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday







user330157













New contributor




user330157 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









user330157user330157

233




233




New contributor




user330157 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





user330157 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






user330157 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1




    Could you clarify what’s wrong about the sort order used in your example output? It seems to be sorted correctly...
    – Stephen Kitt
    yesterday










  • @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
    yesterday










  • 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
    yesterday










  • 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
    yesterday










  • @StephenKitt I asked the wrong question. Thank you.
    – user330157
    yesterday














  • 1




    Could you clarify what’s wrong about the sort order used in your example output? It seems to be sorted correctly...
    – Stephen Kitt
    yesterday










  • @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
    yesterday










  • 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
    yesterday










  • 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
    yesterday










  • @StephenKitt I asked the wrong question. Thank you.
    – user330157
    yesterday








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
yesterday




Could you clarify what’s wrong about the sort order used in your example output? It seems to be sorted correctly...
– Stephen Kitt
yesterday












@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
yesterday




@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
yesterday












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
yesterday




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
yesterday












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
yesterday




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
yesterday












@StephenKitt I asked the wrong question. Thank you.
– user330157
yesterday




@StephenKitt I asked the wrong question. Thank you.
– user330157
yesterday










1 Answer
1






active

oldest

votes


















4














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.






share|improve this answer























  • 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
    yesterday










  • @Kusalananda that’s a bit of a brown-paper-bag moment there (we need that as a hat this December)...
    – Stephen Kitt
    yesterday










  • Thank you. Any idea why sort -t$'t' -nk2 min_ex does not sort by the numerical column?
    – user330157
    yesterday










  • @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
    yesterday












  • @user330157 also -nk2 is not the same as -kn2.
    – mosvy
    yesterday











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
});


}
});






user330157 is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















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









4














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.






share|improve this answer























  • 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
    yesterday










  • @Kusalananda that’s a bit of a brown-paper-bag moment there (we need that as a hat this December)...
    – Stephen Kitt
    yesterday










  • Thank you. Any idea why sort -t$'t' -nk2 min_ex does not sort by the numerical column?
    – user330157
    yesterday










  • @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
    yesterday












  • @user330157 also -nk2 is not the same as -kn2.
    – mosvy
    yesterday
















4














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.






share|improve this answer























  • 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
    yesterday










  • @Kusalananda that’s a bit of a brown-paper-bag moment there (we need that as a hat this December)...
    – Stephen Kitt
    yesterday










  • Thank you. Any idea why sort -t$'t' -nk2 min_ex does not sort by the numerical column?
    – user330157
    yesterday










  • @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
    yesterday












  • @user330157 also -nk2 is not the same as -kn2.
    – mosvy
    yesterday














4












4








4






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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered yesterday









Stephen KittStephen Kitt

165k24366446




165k24366446












  • 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
    yesterday










  • @Kusalananda that’s a bit of a brown-paper-bag moment there (we need that as a hat this December)...
    – Stephen Kitt
    yesterday










  • Thank you. Any idea why sort -t$'t' -nk2 min_ex does not sort by the numerical column?
    – user330157
    yesterday










  • @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
    yesterday












  • @user330157 also -nk2 is not the same as -kn2.
    – mosvy
    yesterday


















  • 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
    yesterday










  • @Kusalananda that’s a bit of a brown-paper-bag moment there (we need that as a hat this December)...
    – Stephen Kitt
    yesterday










  • Thank you. Any idea why sort -t$'t' -nk2 min_ex does not sort by the numerical column?
    – user330157
    yesterday










  • @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
    yesterday












  • @user330157 also -nk2 is not the same as -kn2.
    – mosvy
    yesterday
















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
yesterday




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
yesterday












@Kusalananda that’s a bit of a brown-paper-bag moment there (we need that as a hat this December)...
– Stephen Kitt
yesterday




@Kusalananda that’s a bit of a brown-paper-bag moment there (we need that as a hat this December)...
– Stephen Kitt
yesterday












Thank you. Any idea why sort -t$'t' -nk2 min_ex does not sort by the numerical column?
– user330157
yesterday




Thank you. Any idea why sort -t$'t' -nk2 min_ex does not sort by the numerical column?
– user330157
yesterday












@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
yesterday






@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
yesterday














@user330157 also -nk2 is not the same as -kn2.
– mosvy
yesterday




@user330157 also -nk2 is not the same as -kn2.
– mosvy
yesterday










user330157 is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















user330157 is a new contributor. Be nice, and check out our Code of Conduct.













user330157 is a new contributor. Be nice, and check out our Code of Conduct.












user330157 is a new contributor. Be nice, and check out our Code of Conduct.
















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

An IMO inspired problem

Management

Investment