Score:0

How to sort characters with their occurrence in a file, alphabetically and also when the amount of occurrence is the same?

ar flag

So I have, for example, the following output on the Ubuntu terminal:

238 E
150 A
88 T
88 R

Now I want to sort it from the highest occurrence to the least which I have already done. In addition I also want to do this, for example where T and V come forward 88 times and when this happens, I want to sort those characters alphabetically. So here we get, first 88 R and then comes 88 T. How can I do it on the terminal using commands and/or pipes. (I'm using zsh with Ubuntu if anyone requires this information)

Thanks for you help.

waltinator avatar
it flag
Read `man sort`. You can sort numerically by the first space-delimited field and alphabetically the second field.
hr flag
The default behavior of the GNU `sort -k1,1nr` command should include a "last-resort" lexical sort **unless** you specify `-s` / `--stable`
IPushedU avatar
ar flag
Replying to @waltinator: thanks for your reply. I've read the man page of sort multiple times and my problem is also very specific so I couldn't find anything useful there. I would like to know more about "the first space-delimited field and alphabetically the second field", on how to do it. But what I needed was that if and if only two or more same amount of occurrence of character are present, then to only sort those characters alphabetically.
IPushedU avatar
ar flag
Replying to @steeldriver: thank you too for your help. I've found the answer/solution by using sort -k 1nr before I had seen your comment. I've seen this option for sort many places but I don't fully understand what it is/does. (Newbie here). Could you maybe also explain what -s/--stable does as I've neer seen/used it myself? Thanks a lot.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.