sorting data based on multi columns


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users sorting data based on multi columns
# 1  
Old 02-15-2007
sorting data based on multi columns

Hi all

I have data in following format:

CSCH74,2007,1,09103,15
CSCH74,2007,10,09103,0
CSCH74,2007,11,09103,0
CSCH74,2007,12,09103,0
CSCH74,2007,2,09103,15
CSCH74,2007,3,09103,194
CSCH74,2007,4,09103,115
CSCH74,2007,5,09103,66
CSCH74,2007,6,09103,0
CSCH74,2007,7,09103,0
CSCH74,2007,8,09103,0
CSCH74,2007,9,09103,0
CSCS74,2007,1,09103,142
CSCS74,2007,10,09103,0
CSCS74,2007,11,09103,0
CSCS74,2007,12,09103,0
CSCS74,2007,2,09103,1224
CSCS74,2007,3,09103,1332
CSCS74,2007,4,09103,1169
CSCS74,2007,5,09103,195
CSCS74,2007,6,09103,0
CSCS74,2007,7,09103,0
CSCS74,2007,8,09103,0
CSCS74,2007,9,09103,0

I want to sort it so that output is ( basically sort on column1 and 3)

CSCH74,2007,1,09103,15
CSCH74,2007,2,09103,15
CSCH74,2007,3,09103,194
CSCH74,2007,4,09103,115
CSCH74,2007,5,09103,66
CSCH74,2007,6,09103,0
CSCH74,2007,7,09103,0
CSCH74,2007,8,09103,0
CSCH74,2007,9,09103,0
CSCH74,2007,10,09103,0
CSCH74,2007,11,09103,0
CSCH74,2007,12,09103,0
CSCS74,2007,1,09103,142
CSCS74,2007,2,09103,1224
CSCS74,2007,3,09103,1332
CSCS74,2007,4,09103,1169
CSCS74,2007,5,09103,195
CSCS74,2007,6,09103,0
CSCS74,2007,7,09103,0
CSCS74,2007,8,09103,0
CSCS74,2007,9,09103,0
CSCS74,2007,10,09103,0
CSCS74,2007,11,09103,0
CSCS74,2007,12,09103,0

I have tried :

sort -t, +2 -3 +0 -1 file
sort -t, -k3,3 n -k1,1 file
sort -t"," -k3,3 n -k1,1 file

result is not what I wanted

CSCH74,2007,1,09103,15
CSCS74,2007,1,09103,142
CSCX74,2007,1,09103,28
CSYH74,2007,1,09103,57
CSYS74,2007,1,09103,165
CSYX74,2007,1,09103,11
D13H41,2007,1,08475,5
D13H42,2007,1,08475,43
D13H62,2007,1,08475,1
D13H81,2007,1,08475,12
D13L41,2007,1,08475,0


sort -t, -k1,1 -k3,3 n file
sort -t"," -k1,1 -k3,3 n file
sort -t"," -k 1,1 -k 3,3 n file

for all the above command the output is same as input file with statement cannot open 'n' and in last case cannot open -k 3,3 and n.

sort -t, -k1,1 -k3,3 -n file (comes up with usage of sort command)


nothing seems to work. any help from Unix Gurus is appreciated.

Thanks
Sumeet
# 2  
Old 02-15-2007
Code:
sort -t ',' -k1.1,1.8 -k3.1,3.2 file

# 3  
Old 02-15-2007
Code:
sort -t"," -k1.1,1.6 -k3n file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split multi columns line to 2 columns

I have data like this 1 a,b,c 2 a,c 3 b,d 4 e,f would like convert like this 1 a 1 b 1 c 2 a 2 c 3 b 3 d 4 e 4 f Please help me out (4 Replies)
Discussion started by: jhonnyrip
4 Replies

2. Shell Programming and Scripting

Multi line sorting in Linux

I have log files with following format - YYYY/MM/DD HH:mm:ss.msec|field2|filed3| log message Now the message itself can be multi line message containing new line character. for e.g. 2013/02/05 15:33:12.234|abc|xyz| This is first single line message. 2013/02/05 15:33:12.786|abc|xyz| This... (6 Replies)
Discussion started by: gini32
6 Replies

3. Shell Programming and Scripting

awk based script to find the average of all the columns in a data file

Hi All, I need the modification for the below mentioned code (found in one more post https://www.unix.com/shell-programming-scripting/27161-script-generate-average-values.html) to find the average values for all the columns(but for a specific rows) and print the averages side by side. I have... (4 Replies)
Discussion started by: ks_reddy
4 Replies

4. Shell Programming and Scripting

Awk based script to find the median of all individual columns in a data file

Hi All, I have some data like below. Step1,Param1,Param2,Param3 1,2,3,4 2,3,4,5 2,4,5,6 3,0,1,2 3,0,0,0 3,2,1,3 ........ so on Where I need to find the median(arithmetic) of each column from Param1...to..Param3 for each set of Step1 values. (Sort each specific column, if the... (5 Replies)
Discussion started by: ks_reddy
5 Replies

5. UNIX for Dummies Questions & Answers

Sorting data in file based on field in another file

Hi, I have two files, one of which I would like to sort based on the order of the data in the second. I would like to do this using a simple unix statement. My two files as follows: File 1: 12345 1 2 2 2 0 0 12349 0 0 2 2 1 2 12350 1 2 1 2 2 2 . . . File2: 12350... (3 Replies)
Discussion started by: kasan0
3 Replies

6. Shell Programming and Scripting

sorting multi dimensional array

Hi there, Can someone let me know how to sort the 2 dimensional array below by column 1 then by column 2? 22 55 2222 2230 33 66 44 58 222 240 11 25 22 60 33 45 output: 11 25 22 55 22 60 33 45 33 66 44 58 (6 Replies)
Discussion started by: phoeberunner
6 Replies

7. Shell Programming and Scripting

How to convert 2 column data into multiple columns based on a keyword in a row??

Hi Friends I have the following input data in 2 columns. SNo 1 I1 Value I2 Value I3 Value SNo 2 I4 Value I5 Value I6 Value I7 Value SNo 3 I8 Value I9 Value ............... ................ SNo N (1 Reply)
Discussion started by: ks_reddy
1 Replies

8. Shell Programming and Scripting

Sorting based on Multiple columns

Hi, I have a requirement whereby I have to sort a flat file based on Multiple Columns (similar to ORDER BY Clause of Oracle). I am getting 10 columns in the flat file and I want the file to be sorted on 1st, 3rd, 4th, 7th and 9th columns in ascending order. The flat file is pipe seperated. Any... (15 Replies)
Discussion started by: dharmesht
15 Replies

9. Shell Programming and Scripting

sorting file based on two or more columns

Hi gang. I'm using a unix/mac system and i'm trying to sort a file (more than 1,000,000 lines). chr1 100000965 100001001 - chr1 100002155 100002191 + chr1 100002165 100002201 + chr1 100002525 100002561 - chr1 10000364 ... (2 Replies)
Discussion started by: labrazil
2 Replies

10. Shell Programming and Scripting

Sorting based on columns

Hi, I want a list of entries in 3 space delimited columns. I want to sort entries based on the very first column. Rows can't be changed. For example: If I have... Abc Abc Acc Bca Bda Bdd Cab Cab Cbc Dbc Dca Dda Abc Abc Acc the output should be... Abc Abc Acc Abc Abc Acc Bca... (7 Replies)
Discussion started by: MobileUser
7 Replies
Login or Register to Ask a Question