Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a delimited text file of multiple columns some of which are text and some of which are numbers. I would like to sort on multiple fields, wich span both text and numerical fields. While -n allows treating things as numbers, it does strange things with text, and omitting that results in numbers being treated like text. Is there a way I can associate a field with a treatment?

EXAMPLE:

Input file:

Doggy,20
Cat,20
Cat,5
Bird,100
Bat,5

Desired sort based on first field, then second:

Bat,5
Bird,100
Cat,5
Cat,20
Doggy,20

Result of sort input.txt -t, -k1,1 -k2,2:

Bat,5
Bird,100
Cat,20
Cat,5
Doggy,20

note: Cat,20 precedes Cat,5

Result of sort input.txt -t, -n -k1,1 -k2,2:

Bat,5
Cat,5
Cat,20
Doggy,20
Bird,100

Note: Bird,100 is last

share|improve this question

migrated from stackoverflow.com Oct 11 '12 at 7:18

This question came from our site for professional and enthusiast programmers.

1 Answer 1

up vote 3 down vote accepted
sort input.txt -t, -k1,1 -k2,2n

or

sort input.txt --field-separator=, --key=1,1 --key=2,2n
share|improve this answer

Your Answer

 
discard

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.