how do I sort a file based on multiple columns

how do I sort a file based on multiple columns

Post by Ramk » Thu, 22 Jun 2006 09:31:38



I am newbi to perl. I have a text file which has couple of columns
(col1, col2, col3, col4, col5, col6 etc) and this file has 10000 lines.
how do I sort this file based on col1, col3 and col5.

I appriciate your help.

Thanks

 
 
 

how do I sort a file based on multiple columns

Post by Barry Margoli » Thu, 22 Jun 2006 13:07:18




> I am newbi to perl. I have a text file which has couple of columns
> (col1, col2, col3, col4, col5, col6 etc) and this file has 10000 lines.
> how do I sort this file based on col1, col3 and col5.

> I appriciate your help.

> Thanks

What does perl have to do with it?  Use the sort command:

sort -k 1,1 -k 3,3 -k 5,5 filename > filename.sorted

--

Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***

 
 
 

1. SORT command > sorting decimal numbers in columns on a file not working !

Hi. I try to sort a column on a file containing decimal numbers. I use
Sse Linux 9.1. Then I try to do this:


doasidpoas   opipo     7.8
kjkljkljklji   kjklj   35.7
kljkljkl       kljkl   2.3
dopasidp       poipo   0.2
kjkljlkl       kjkl    0.0
kljkljkjl      ljlk    0.8
jklhkljkl      kjkl    0.0
kljkljkljl     kjkl    0.1

dopasidp       poipo   0.2
jklhkljkl      kjkl    0.0
kjkljlkl       kjkl    0.0
kljkljkjl      ljlk    0.8
kljkljkljl     kjkl    0.1
kljkljkl       kljkl   2.3
doasidpoas   opipo     7.8
kjkljkljklji   kjklj   35.7

kjkljkljklji   kjklj   35.7
doasidpoas   opipo     7.8
kljkljkl       kljkl   2.3
kljkljkljl     kjkl    0.1
kljkljkjl      ljlk    0.8
kjkljlkl       kjkl    0.0
jklhkljkl      kjkl    0.0
dopasidp       poipo   0.2

As you can see the sorting order is not correct. What am I doing wrong ?
I also tried to use '-g' option instead of '-n' but doesnt work.

Any idea ?

Thanks
Alberto

2. rotatelogs.c? how do I implement this?

3. Sorting By Second Column With Unique First Column

4. Red Hat 4.0 with ncurses....

5. sorting multiple columns in different order

6. International kernel patch for 2.4.7

7. How to have sort start sorting froma given column?

8. CONVERSION, (postscript to laser) on 3B's and DEC stations

9. Newbie: adding and averaging columns, based on feature in column 2 (awk?)

10. Sorting a 2 column text file

11. Help to sort a file on particlular columns

12. How to sort variable columns file

13. sed/awk : need just the first column in a multi-column file