1

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
1
  • +1 for question with copy/paste test setup. But why are you using -k 1.4>>,1<<n ? Isn't the comma a 'range' operator, where you'd expect the 2nd part of the range to be value greater than the first? Good luck! Commented Jul 26, 2013 at 21:56

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.