3

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
0

1 Answer 1

7

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 .