Score:0

sorting by different columns in a sequence or sorting by multiple columns at once

dk flag

What is the difference between sort -t' ' -k1,1n -k2,2n and sort -t' ' -k1,2n?

Could you give examples?

I've tried:

logan@logan-mainPC:~/my-test/learn-sort$ cat myage 
my age 1
my age 100
my age 2
my age 200
logan@logan-mainPC:~/my-test/learn-sort$ cat myage | sort -k2,3
my age 1
my age 100
my age 2
my age 200
logan@logan-mainPC:~/my-test/learn-sort$ cat myage | sort -k2,2 -k3,3
my age 1
my age 100
my age 2
my age 200
logan@logan-mainPC:~/my-test/learn-sort$ cat myage | sort -k2,2 -k3,3n
my age 1
my age 2
my age 100
my age 200

I think sort -k2,3 sorts 'age 1', 'age 100', ... and sort -k2,2 -k3,3 sorts 'age', 'age',... then '1', '100', .... Since they all treat columns as strings they produce the same outcome.

But sort -k2,2 -k3,3n produces different outcome because it treats column 3 as numbers.

But then:

logan@logan-mainPC:~/my-test/learn-sort$ cat myage | sort -k2,3n
my age 1
my age 100
my age 2
my age 200

which is strange. Found out the reason is because column 2 is not numbers.

logan@logan-mainPC:~/my-test/learn-sort$ cat myage | sort --debug -k2,3n
sort: using ‘en_AU.UTF-8’ sorting rules
sort: key 1 is numeric and spans multiple fields
my age 1
   ^ no match for key
________
my age 100
   ^ no match for key
__________
my age 2
   ^ no match for key
________
my age 200
   ^ no match for key
__________

Thanks.

24601 avatar
in flag
read [ask] then [edit] your question accordingly with details of the research you have done to find an answer.
Logan Lee avatar
dk flag
@24601 yep i edited the question thx!
Score:0
dk flag

I can see what is happening by --debug flag.

sort -k2,3 indeed sorts 'age 1', 'age 100',...

logan@logan-mainPC:~/my-test/learn-sort$ cat myage | sort --debug -t' ' -k2,3
sort: using ‘en_AU.UTF-8’ sorting rules
my age 1
   _____
________
my age 100
   _______
__________
my age 2
   _____
________
my age 200
   _______
__________

sort -k2,2 -k3,3 sorts by 'age',... then '1', '100', ...

logan@logan-mainPC:~/my-test/learn-sort$ cat myage | sort --debug -t' ' -k2,2 -k3,3
sort: using ‘en_AU.UTF-8’ sorting rules
my age 1
   ___
       _
________
my age 100
   ___
       ___
__________
my age 2
   ___
       _
________
my age 200
   ___
       ___
__________

Lastly

logan@logan-mainPC:~/my-test/learn-sort$ cat myage | sort --debug -t' ' -k2,2 -k3,3n
sort: using ‘en_AU.UTF-8’ sorting rules
my age 1
   ___
       _
________
my age 2
   ___
       _
________
my age 100
   ___
       ___
__________
my age 200
   ___
       ___
__________

treats third column as numbers.

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.