FGV
FGV

Reputation: 165

sorting alphanumeric multiple columns

I'm trying to sort this file by field 1 (ignoring the ccc) and when equal, by field 2 as a number:

ccc1    4
cccY    1
cccY    5
cccX    2
cccX    10
ccc10   4
ccc1    10
ccc2    5

I'm running sort as sort -t $'\t' -k 1.4,1n -k 2,2n" but, I'm not getting the expected output:

cccY    1
cccX    2
cccY    5
cccX    10
ccc1    4
ccc1    10
ccc2    5
ccc10   4

Why is the cccY and cccX mixed? It should give first both cccX and then both cccY, right?

thanks,

FGV

PS - In case you want to reproduce it, just run:

echo -e "ccc1\t4\ncccY\t1\ncccY\t5\ncccX\t2\ncccX\t10\nccc10\t4\nccc1\t10\nccc2\t5" | sort -t $'\t' -k 1.4,1n -k 2,2n

Upvotes: 1

Views: 173

Answers (1)

Wirus
Wirus

Reputation: 1190

I hope this is what you need.

echo -e "ccc1\t4\ncccY\t1\ncccY\t5\ncccX\t2\ncccX\t10\nccc10\t4\nccc1\t10\nccc2\t5" | sort -t $'\t' -k 1.4,1 --version-sort -k 2n

I believe the main problem in your command is with numeric sort in first key, because there are numbers mixed with chars.

Upvotes: 1

Related Questions