Sorting based on Multiple columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting based on Multiple columns
# 8  
Old 05-12-2009
Code:
sort -t '|' -k1n -k4n -k3rn -k2nr abc.txt

# 9  
Old 05-12-2009
Vn

vgersh,

I'm sorry to say that it doesn't work...I'm trying to be clear here

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

should be sorted on 0th field i.e 1705614
1705614
Then on 4th field i.e C ( C is should come first)
U
Should both be the 'C' s,
sort on 3rd field i.e 99 (Ascending)
88
Should both be the same numbers,
sort on 2nd field i.e 2 (descending)
1
I hope I am clear...Thanks
# 10  
Old 05-12-2009
Fields and characters within fields are numbered starting with 1
Code:
sort -t '|' -k1n,1 -k5,5 -k4n,4 -k3n,3r myFile

# 11  
Old 05-12-2009
Vn

Vgersh,

I appreciate your help...Seems working...Thank you so much
# 12  
Old 05-12-2009
Vn

vgersh,

Hope I'm not bothering you...one last question for now

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

Assuming all those previously sorted fields equal < can i sort the above
to get the second row up since the date is recent...

Thanks again...
# 13  
Old 05-12-2009
Code:
nawk -F'|' '{n=split($NF, a, "[ /]"); gsub(":", "", a[n]);printf("%d%02d%02d%s%s\n", a[3], a[1], a[2], a[n], OFS $0)}' OFS='|' myFile | sort -rn -k1,1 | cut -d '|' -f2-


Last edited by vgersh99; 05-12-2009 at 06:04 PM..
# 14  
Old 05-13-2009
sample.txt file
1873072|PAC|2|3|C|1/4/2009 0:00:00
1873072|PA|2|3|U|1/9/2009 0:00:00

nawk -F'|' '{n=split($NF, a, "[ /]"); gsub(":", "", a[n]);print a[3] a[1] a[2] a[n] OFS $0}' OFS='|' sample.txt | sort -rn -k1,1 | cut -d '|' -f2-

gives

1873072|PA|2|3|U|1/9/2009 0:00:00
1873072|PAC|2|3|C|1/4/2009 0:00:00

but In the above case I need the data in reverse, because column 5 should be preferred for 'C', only in case if both are 'C's or 'U's then i want this to be sorted on the last column i.e date

Thanks
-venkat
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