sort on multiple fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sort on multiple fields
# 1  
Old 04-23-2009
sort on multiple fields

hello all

I have a file names xxx with data like

1,2,3,12
1,3,6,12
1,3,5,12
2,4,6,12
6,5,6,12
4,2,7,12
4,1,3,12

I wish to sort this file xxx on first three fields in ascending order.

OUPUT should be like

1,2,3,12
1,3,5,12
1,3,6,12
2,4,6,12
4,1,3,12
4,2,7,12
6,5,6,12

how can i sort the file on all the field
# 2  
Old 04-23-2009
I used sort and gave the output you provided above....

Code:
$ cat test.txt
1,2,3,12
1,3,6,12
1,3,5,12
2,4,6,12
6,5,6,12
4,2,7,12
4,1,3,12

$ sort test.txt
1,2,3,12
1,3,5,12
1,3,6,12
2,4,6,12
4,1,3,12
4,2,7,12
6,5,6,12

# 3  
Old 04-23-2009
Quote:
Originally Posted by vasuarjula
hello all

I have a file names xxx with data like

1,2,3,12
1,3,6,12
1,3,5,12
2,4,6,12
6,5,6,12
4,2,7,12
4,1,3,12

I wish to sort this file xxx on first three fields in ascending order.

OUPUT should be like

1,2,3,12
1,3,5,12
1,3,6,12
2,4,6,12
4,1,3,12
4,2,7,12
6,5,6,12

how can i sort the file on all the field

Try sort command. Check man pages for more info.
# 4  
Old 04-24-2009
It's always a good practice to use -n option while sorting numbers....

Code:
sort -n filename

# 5  
Old 04-24-2009
Code:
cat yourfile | awk -F"," '{print $1$2$3","$0}' | sort -t"," -n | cut -d"," -f2,3,4,5

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

2. Shell Programming and Scripting

Sort unique by multiple fields

i need to sort to get all the unique records based on the 1st and 2nd column, and keep the record with the highest value on 5th column if there are duplicates, every column with varies length a^2^x^y^z bxc^2xx2^aa^bvxxxx^cdd a^3^1^2^3 a^2^x^1^c I want a result which will only keep the 1st... (2 Replies)
Discussion started by: dtdt
2 Replies

3. Shell Programming and Scripting

Sort on first fields only

Please advice in this. Input file 100,vvvt 201,unb 100,sos 301,abc 99,gang desired output 99,gang 100,vvvt 100,sos 201,unb 301,abc Means if first fields are same ( here 100) the do not sort. (4 Replies)
Discussion started by: theshashi
4 Replies

4. Shell Programming and Scripting

doing a sort on first two fields

hi, i'm having a file stg_ff.txt which contains 10 fields,which contains millions of records i need to cat the first 10 rows in the file after doing a sorting on the first two fields i n the file. can any body help me on this. regards Angel (4 Replies)
Discussion started by: angel12345
4 Replies

5. UNIX for Dummies Questions & Answers

Sort on first two fields -only-

Hey, I have a file i want to sort. It contains these kind of lines: FirstName LastName buyID buyType Eg: John Doe 22 Car Jane Simpson 4 Headset John Doe 11 Telephone Now if I use the sort command on it cat purchases.txt | sort -k1,1 -k2,2 it would also sort the third and 4th field:... (4 Replies)
Discussion started by: ce3c
4 Replies

6. UNIX for Dummies Questions & Answers

sort on three fields

Hi I would like to sort a csv file. It has 50 fields and approx 1400000 lines. I want to sort by three columns as follows. Say sort on coulmn 5, if entries are equal sort on column 3 if this is also equal sort on column 6. It seems that this is possible for 5 then 3 i.e. sort -t "," -k 5,5 -k... (1 Reply)
Discussion started by: turquoise_man
1 Replies

7. Shell Programming and Scripting

sort on multiple fields

Hello All I have data in a flat file with numeric and aplha numeric datatypes. Now i have to sort on multiple fileds. Can any body please give me the sort code? i am particularly confused about the sort code like sort -n +0 -1 +1 -2 .... (1 Reply)
Discussion started by: vasuarjula
1 Replies

8. UNIX for Dummies Questions & Answers

Unix sort on multiple fields

Hello. I've read a few threads on how to sort on multiple fields, but I still can't get my file to sort correctly. I have a comma delimited .csv file will over a hundred fields. I want to sort it by field 2, field 62 and then field 61 (integer fields). input looks like this well swap field... (2 Replies)
Discussion started by: happy_cow
2 Replies

9. Shell Programming and Scripting

sort on fields

Hi I have following fixed width file and I have to sort on 2 fields ABC 111222333002555 77788 ABC 111222333004555 77788 ABC 111222333001555 77788 ABC 111222333003555 77788 ABC 111222333005555 77788 one is from field1 to field 3 "ABC" and another is on 14 to 16 "002" (based on first... (1 Reply)
Discussion started by: mgirinath
1 Replies

10. Shell Programming and Scripting

sort fields

I have a large csv file that looks like this: 14 ,M0081,+000000001,200302,+00000100500, 14 ,M0081,+000000004,200301,+00000100500, 14 ,M0081,+000000005,200305,+00000100500, 14 ,M0081,+000000000,200205,+00000100500, 14 ,M0081,+000000008,200204,+00000100500, ... (2 Replies)
Discussion started by: edog
2 Replies
Login or Register to Ask a Question