Unix sort on multiple fields


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Unix sort on multiple fields
# 1  
Old 12-12-2008
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 61 before 62 (sorry):
field 2 field 62 field 61
4609 0 100
48223 -9567 3304
48289 0 3023
48339 -9525 3008
48491 -9754 3054
51610 -7716 3008
51610 \N (this is NULL) \N (this is NULL)
51610 -7716 3008
51610 \N (this is NULL) \N (this is NULL)
53033 -12234 4769

i used:

sort -n -t ',' -k 2,2 -k 62,62 -k 61,61 -T /tempdirectory/temp input.csv -o output.csv

in the results field 2 sorted correctly, but the others are not sorting the nulls correctly. It plops it right in the middle of the same data. For example, it will be 123, 123, null,null, 123, 123 instead of null, null, 123, 123, 123, 123.

Help anyone?
# 2  
Old 12-12-2008
tah might be because you are using -n option.. if you observe man page of sort you will find this
Quote:
-n Numeric sorting of a field containing any nonnumeric character gives unpredictable results
so choose the correct sort option.
# 3  
Old 12-12-2008
Thank you.

I added the -b option to ignore the blanks. This option worked. It sorted the numeric fields and the null fields in the correct order.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Modify a file content in UNIX and sort for only required fields ?

I have the below contents in a file after making the below curl call curl ... | grep -E "state|Rno" | paste -sd',\n' | grep "Disconnected" > test "state" : "Disconnected",, "Rno" : "5554f1d2" "state" : "Disconnected",, "Rno" : "10587563" "state" : "Disconnected",, "Rno" :... (2 Replies)
Discussion started by: Vaibhav H
2 Replies

2. 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

3. UNIX for Dummies Questions & Answers

UNIX multiple column sort

Hi, I have a text file that has data like this chr1 156106712 156106819 LMNA 8 + 1 147 chr1 156106712 156106819 LMNA 8 + 2 147 chr1 156106712 156106819 LMNA 8 + 3 148 chr1 ... (4 Replies)
Discussion started by: mitt
4 Replies

4. 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

5. Shell Programming and Scripting

UNIX append field with comparing fields from multiple column

I have a csv dump from sql server that needs to be converted so it can be feed to another program. I already sorted on field 1 but there are multiple columns with same field 1 where it needs to be compared against and if it is same then append field 5. i.e from ANG SJ,0,B,LC22,LC22(0) BAT... (2 Replies)
Discussion started by: nike27
2 Replies

6. Shell Programming and Scripting

Sort based on Multiple Columns in UNIX

Hi, I would like to sort a list in different ways: 1> Unique based on Field 1 with highest Field 4 For Instance Input: 1678923450;11112222333344;11-1x;2_File.xml 1678923450;11112222333344;11-1x;5_File.xml 1234567890;11113333222244;11-1x;3_File.xml Output: ... (7 Replies)
Discussion started by: DevendraG
7 Replies

7. Shell Programming and Scripting

Count of Multiple Fields(After Validation) in UNIX

Hi , i have scenario where i have to get count of each field ,if the field have value equal to "A" For Example : Field_01 Field_02 A A A B B A A A OUTPUT : Field_01 Field_count ... (2 Replies)
Discussion started by: kkabc789
2 Replies

8. Shell Programming and Scripting

sql loader for inserting the data from multiple fields from unix

Hi , I have my log file something like this (07/29/2009 00:02:24.467) 367518 (07/29/2009 00:02:26.214) 949384011 (07/29/2009 00:02:26.236) 367524 (07/29/2009 00:02:28.207) 949395117 (07/29/2009 00:02:28.240) 337710 (07/29/2009 00:02:30.621) 949400864 I am trying to insert the data... (3 Replies)
Discussion started by: rdhanek
3 Replies

9. 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

10. Shell Programming and Scripting

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 Replies)
Discussion started by: vasuarjula
4 Replies
Login or Register to Ask a Question