1833870 Members
1676 Online
110063 Solutions
New Discussion

Re: sorting question

 
SOLVED
Go to solution
dictum9
Super Advisor

sorting question


I want to sort output by the certain column, ie. 4th column. From the man page, the necessary option seems to be -k, so that

cat file | sort -k 5

It sorts it in a different way but it doesn't work. Neither does

cat file | sort -k 5.3

6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: sorting question

Several things could be wrong. 1) You haven't specified what the separator charactor is so that is the 5th field may not be what sort thinks is the 5th field. Ialso you may need a type specifier such as -k 5n,5 to force numeric comparisons rather than lexical comparisons. For example, if 'n' were not used (and assumming this us the 5th field)
1
2
3
10
20
100

would actually be sorted as

1
10
100
2
20
3
If it ain't broke, I can fix that.
Patrick Wallek
Honored Contributor

Re: sorting question

Can you show us a sample of the file you want to sort? Just a few lines will do. This may help in figuring out how to get it sorted.
Sung Oh
Respected Contributor

Re: sorting question

if you are sorting by 4th column, you should use

cat file | sort -k 4

if this still does not work can you print the outout to here?

Sung
Ivan Ferreira
Honored Contributor

Re: sorting question

I just use:

cat file | sort +3

Because the columns starts at 0. If you are doinig numeric sorting, use -n, like this:

cat file | sort +3 -nb
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
dictum9
Super Advisor

Re: sorting question

-n flag does the trick, thanks.

( As usual, the man page is cryptic )
Arturo Galbiati
Esteemed Contributor

Re: sorting question

Hi,
why not sort -n +3 file avoiding cat?
HTH,
Art