0

Here is my file, the first line is not part of it (the one with Fname ..)

Fname Lname Age Children 
John  Doe   61  2
Mary  Jane  32  5
Delta Cross 30  5
Sui   Ace   24  1

I want to sort this file based on number of children. It should be sorted from most children to least children.

Here is my command: sort -k 4 -n -r file.txt, which says sort based on the 4th column, numeric sort, and reverse. This yields:

Mary  Jane  32  5
Delta Cross 30  5
John  Doe   61  2
Sui   Ace   24  1

The way my text does it is sort -k4nr file.txt. I have two questions regarding this:

Firstly, I thought our options must be separated by spaces (not joined together)? Is this not the case for sort, or not the case for any of the commands?

Secondly, when doing it this way, I get the "right" order, but Delta is first:

Delta Cross 30  5
Mary  Jane  32  5
John  Doe   61  2
Sui   Ace   24  1

Why is this?

marked as duplicate by muru, shirish, Kusalananda, Toby Speight, Timothy Martin May 15 '18 at 16:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2
  1. GNU utilities often have the ability to parse single-letter options as a single hyphen-led group, such that -n -r can also be sent as -nr; this is only the case when there is not already an nr option that it could be mistaken for.

  2. Using -k 4 -n -r tells sort that you want to sort on field 4, and you'd like every line in the file to be sorted in reverse numerical order. Once sort is done sorting the two 5-key lines on their equal key value, it sorts the entire line, described by the man page:

    Finally, as a last resort when all keys compare equal, sort compares entire lines as if no ordering options other than --reverse (-r) were specified.

... which is why Mary then comes before Delta.

When you instead use -k4nr, you tell sort to use field 4 and to sort only that field in reverse numerical order. When sort is done comparing 5 with 5, it sorts those two (entire) lines in normal dictionary order (since there is no global reverse option here), and so Delta comes before Mary.

0

Depending on the command, you can group the options together. sort is one of these but it can also change the output which I'll get to below.

This is also true for rm

rm -rf directory does the same as rm -r -f directory

As for why your two commands give different orders, your first command sorts using the 4th column as the key via -k 4 and then moves on to numeric sort and then reverses it. (You actually don't need the n because the 4th column only contains numbers any way so sort -k 4 -r will give the same output). It then sorts the rest of the columns in reverse order.

Your second command groups them together and then operates by sorting only the 4th column by reverse numerical order. They both have the same key value for the 4th column so it moves on to sorting in alphabetical order for the first two columns.

If you change this line:

Delta Cross 30 5

to

Zelta Cross 30 5

then the line beginning with Mary will come first for the second command.

Not the answer you're looking for? Browse other questions tagged or ask your own question.