1

file: nameslist.txt

Emily 0
Emily 1
Emily 5
Joe 0
Joe 10
Joe 5
Joe 6

This is the command I ran:

cat nameslist.txt | sort -k1 -k2

The result:

Emily 0
Emily 1
Emily 5
Joe 0
Joe 10
Joe 5
Joe 6

It looks like it's sorting by the first number, but how can I get the second column to sort numerically? The result I want is:

Emily 0
Emily 1
Emily 5
Joe 0
Joe 5
Joe 6
Joe 10

marked as duplicate by don_crissti, Community Jan 24 '18 at 18:38

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

Use the -n option to sort numerically:

sort -k1,1 -k2n nameslist.txt

There is no need to use cat to read the input file. sort will do that without needing another process.

The -k1,1 notation sets the start and stop limits of the first sort key to the first field.

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