Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I tried this:

$ echo "2,3435,1" | sort -n

$ sort -t',' -n test_sort.txt

$ cat test_sort.txt

Why doesn't my sort command work?

My desired sort command should work like this:

$sort -t',' -n test_sort.txt  

ANSWER : tr , '\n' < a | sort -n | paste -sd, -

"Thanks to Stéphane Chazelas"

share|improve this question
What doesn't work? What is the desired output? – psimon Jul 2 '14 at 14:46
The -t specifies what separates fields, but sort re-orders rows of data. It just references fields in determining what value to sort by. – Bratchley Jul 2 '14 at 14:46
tr , '\n' < a | sort -n | paste -sd, - – Stéphane Chazelas Jul 2 '14 at 15:10
@Stéphane Chazelas You sir, are really great – SIJAR Jul 2 '14 at 15:39
@StéphaneChazelas You should post that as an answer. Answering questions in comments (something I've been guilty of in the past) is not desirable. – David Conrad Jul 2 '14 at 18:18

Sort works on a per-line basis, not on fields within a line.

By default, it sorts based on the first character on the line and goes on from there. But you can also sort on "keys" other than at the beginning. This is useful when you want to sort on last name, or a numeric value at the end of the line, or so on. That's what the -t flag is for — it won't break up individual lines and sort within them.

If that's what you want to do, see Sort fields inline

share|improve this answer

sort operates on entire lines. By default, it sorts on the entire contents of that line, but -k can be used to sort on one or more fields within those lines. -t can be used to change the delimiter between fields. I can't think of a case where using -t without also using -k makes any sense.

Your second command, which is equivalent to:

printf "%s\n%s\n" "110,20,30,13" "kill,gill,burger" | sort -t',' -n



Which is what I'd expect. -t',' has no effect because it's changing the field delimiter when you haven't told sort to operate on individual fields, and so k is sorted before 1 because its numerical value is 0 (and you requested numerical ordering using -n).

share|improve this answer

It will be easier with perl:

$ perl -F',' -anle '
    BEGIN { $" = "," }
    print "@{[sort {$a <=> $b} @F]}"
' file 

This only sort lines which contains numbers. If you want sort lines contains string like sort -n does, try:

$ $ perl -MPOSIX=isdigit -F',' -anle '
BEGIN { $" = "," }
print "@{[ sort { isdigit($a)
              ? ($a <=> $b)
              : ($a cmp $b)
              } @F
' file

This approach still only works if like contains only string,failed with like contains mixed string, numbers like kill,gill,20.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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