Sorting based on Multiple columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting based on Multiple columns
# 1  
Old 12-11-2003
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 help will be highly appreciated.

Thanks,
Dharmesh
# 2  
Old 12-11-2003
There is a good man-page for your question :

man sort

It does it all Smilie


Regs David
# 3  
Old 12-12-2003
Hi !

Try to use this command :

sort +1 +3 +4 +7 +9 file_to_be_sorted


witt
# 4  
Old 12-12-2003
From witt

Hi !

Try to use this command :

sort +1 +3 +4 +7 +9 file_to_be_sorted
------------------------------------------------------------------------------------

here sort +1 means sort from the 2nd field, we need to use something like

sort +0 -1 +2 -3 +4 -5 +6 -7 +8 -9 filename

Just try out and let me know the result, I am also telling based on the property of the sort utility, not too sure.

Cheers
JK
# 5  
Old 12-15-2003
hi ,
the commands
sort +1 +3 +4 +7 +9 file_to_be_sorted
and
sort +0 -1 +2 -3 +4 -5 +6 -7 +8 -9 filename
sort on the forst field only
# 6  
Old 12-15-2003
Well I am not too sure what you exactly want the output to be but sort +1 -2 +3 -4 will sort the output based on 2nd field and if second field is same it will see the next specified field in this case it is 4th field.

I had given the examples below

# cat junk [This is a junk file which contains data]
5 3 2 9
3 4 1 7
5 2 3 1
6 1 3 6
1 2 4 5
3 1 2 3
5 2 2 3
# sort +1 -2 +3 -4 junk [sort based on the 2nd and 4th field]
3 1 2 3
6 1 3 6
5 2 3 1
5 2 2 3
1 2 4 5
5 3 2 9
3 4 1 7
# sort +1 -2 junk [sort based on the 2nd field]
3 1 2 3
6 1 3 6
1 2 4 5
5 2 2 3
5 2 3 1
5 3 2 9
3 4 1 7
# sort +1 +3 junk [See here it is same as sort +1 it checks for the 3rd field if the 2nd field is same, it does not go for the 4the field ]
3 1 2 3
6 1 3 6
5 2 2 3
5 2 3 1
1 2 4 5
5 3 2 9
3 4 1 7
# sort +1 junk
3 1 2 3
6 1 3 6
5 2 2 3
5 2 3 1
1 2 4 5
5 3 2 9
3 4 1 7
#

If this is not what you wanted, post your data and the expected output whatever you want, we will look into that

Cheers
JK
# 7  
Old 05-12-2009
Vn

1705614|FNP|2|99|U|1/20/2009 0:00:00
1705614|MSN|1|99|C|1/2/2009 0:00:00

I want the above data to be sorted based on 0 / 4 / 3 / 2 fields (assume above data in abc.txt file)

sort -t '|' +0n +4 +3n +2nr abc.txt
1705614|MSN|1|99|C|1/2/2009 0:00:00
1705614|FNP|2|99|U|1/20/2009 0:00:00

in case my 4 and 3 fields are same I want to sort by field 2 in descending order, that means if the data is

1705614|MSN|1|99|C|1/2/2009 0:00:00
1705614|FNP|2|99|C|1/20/2009 0:00:00

I want this to be flipped like

1705614|FNP|2|99|C|1/20/2009 0:00:00
1705614|MSN|1|99|C|1/2/2009 0:00:00

with the above sort command it doesn't work...please help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help in sorting multiple columns

Hello all, I am using printf to print the sorted o/p in my script.I am trying to sort in following way but doesn't work. printf "%13s %2s UDP %15s:%s Program %4s HD: %23s HD: %23s %10s %s %s %3s days %3s hours\n" $encoder $i "${ipaddr}" ${portno} ${progno} ${inres} ${outres} ${inrate}... (4 Replies)
Discussion started by: ramman
4 Replies

2. UNIX for Dummies Questions & Answers

Sorting by Multiple Columns

Hi I have a text file that has four columns (Logonid,First Name,Last Name,Status) Logonid First Name Last Name Status abc2 Fred Mercury Inactive abc1 John Deacon Active abc3 Roger Taylor Active abc4 Brian ... (2 Replies)
Discussion started by: theref
2 Replies

3. UNIX for Dummies Questions & Answers

Sorting by multiple columns

I have a space delimited text file that I would like to sort by multiple columns. First I want to sort by column 1, then by column 2. Both columns are numerical. Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

4. Shell Programming and Scripting

Sorting based on multiple delimiters

Hello, I have data where words are separated by a delimiter. In this case "=" The number of delimiters in a line can vary from 4to 8. The norm is 4. Is it possible to have a script where the file could be separated starting with highest number of delimiters and ending with the lowest An... (8 Replies)
Discussion started by: gimley
8 Replies

5. Shell Programming and Scripting

Sorting multiple columns

Hi, We have a requirement of need to sort a file based on fields 1,3 and 4. I tried with sort command however it is not giving expected output, can we achieve any other way? Please let me know ASAP. File a e w a a b a a a a d g a a h h c d a e a a a w Output a b a a a a a w a a d... (4 Replies)
Discussion started by: Nagapandi
4 Replies

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

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

8. UNIX for Advanced & Expert Users

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... (2 Replies)
Discussion started by: sumeet
2 Replies

9. UNIX for Dummies Questions & Answers

sorting a file with multiple columns

I have a file with several columns: Column1 Column2 Column3. . .Column6 I want to sort the data from Column6. Could I do that through sort even if there are spaces in between fields? Much thanks! outta. (3 Replies)
Discussion started by: outtacontrol
3 Replies

10. Shell Programming and Scripting

Sorting a flat file based on multiple colums(using character position)

Hi, I have an urgent task here. I am required to sort a flat file based on multiple columns which are based on the character position in that line. I am restricted to use the character position instead of the space and sort +1 +2 etc to do the sorting. I understand that there is a previous... (8 Replies)
Discussion started by: cucubird
8 Replies
Login or Register to Ask a Question