sorting(both Ascending & Descending) files based on multiple fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sorting(both Ascending & Descending) files based on multiple fields
# 1  
Old 05-18-2009
sorting(both Ascending & Descending) files based on multiple fields

Hi All,

I am encountered with a problem while sorting a file based on multiple columns . I need to sort like:
(field2,ascending) , (field3,ascending) ,(field8,descending) , (field7,ascending),(field13,ascending).

So far i was sorting only in ascending order but here i need to use one descending field also.

Please see the sample text which needs to be sorted based on above rule.The text is TAB delimited.


Code:
200902010606	0001319	2113740F35	0	IDEN		0	0	0	0	0	NULL		AK																					
200902010606	0001319	2113740F39	0	IDEN		0	0	0	0	0	NULL		AK																					
200902010606	0001319	2113740F67	0	WLAN		0	0	0	0	0	NULL		AK																					
200902010606	0001319	2113741F25	0	IDEN		0	0	0	0	0	NULL		AK																					
200902010606	0001319	2113743A19	0	GTSS		0	0	0	0	0	NULL		AK																					
200902010606	0001319	2113743K15	0	IDEN		0	0	0	0	0	NULL		AK																					
200902010606	0001319	2113743L13	0	IDEN		0	0	0	0	0	NULL		AK																					
200902010606	0001319	2113743L17	0	IDEN		0	0	0	0	0	NULL		AK

Previously i was using sort like

sort -t "`echo -e "\t"`" +1 -2 +2 -3 >tmp This sort file using 2nd and 3rd field in ascending order only having TAB as delimiter.

Please help me to sort it out.

Thanks
Neeraj

Last edited by radoulov; 05-18-2009 at 08:56 AM.. Reason: added code tags
# 2  
Old 05-18-2009
sort -r reverses the sense of all comparisons.

If you are on linux try msort - it allows you to invert (-i) the sense of the comparison on a field by field basis.

Otherwise you will have to write a sort in perl or C or python... whatever you like.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Sorting a file in descending order when you have 10e- values

Hi, I am trying to sort the following file in descending order of its fourth column. 2 1 363828 -2.423225e-03 3 1 363828 4.132763e-03 3 2 363828 8.150133e-03 4 1 363828 4.126890e-03 I use sort -k4,4g -r input.txt > output.txt ... (1 Reply)
Discussion started by: evelibertine
1 Replies

2. UNIX for Dummies Questions & Answers

sorting sequences in ascending order

Hi, I have this single file with a number of sequence inside it of format >string1 data >string100 data >string10 ..... >string5 ... >string67 ...... the dots represent data. I wanted to get the sequences arranged in ascending order like >string1 data >string5 (5 Replies)
Discussion started by: sonia102
5 Replies

3. Shell Programming and Scripting

Sorting values of hash in ascending order using Perl

I want to sort values of a hash in ascending order. my %records; for my $value (sort values %records){print $value,"\n";} When I use the above code I get values in this order: 1,10,11,2,3,4,5,6,7,8,9. But, I need values in my output in this order: 1,2,3,4,5,6,7,8,9,10,11. Can Someone... (1 Reply)
Discussion started by: koneru_18
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

ascending and descending sort

Hi I have a problem with sort command : sort -nk 1.28,1.34 file | sort -nrk 1.27 file | sort -nk 1.22,1.25 file |sort -nk 1.13,1.21 file | sort -nk 1.9,1.12 file | sort -nk 1.1,1.8 file This is the input file 0000000100010000000200004090317003 0000000100010000000230001020592002... (3 Replies)
Discussion started by: Fafa
3 Replies

6. Shell Programming and Scripting

AWK multiple line fields sorting

I have a bash script which takes a log file with each record separated by a #. The records have multiple fields but field $1 is always the date and time. When the script is run it prints the record just fine from oldest to newest. I need to have records print out from newest first. Here is the... (7 Replies)
Discussion started by: numele
7 Replies

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

8. UNIX for Dummies Questions & Answers

Sort ascending and descending

How can I sort a file as follows ? cols 1 - 10 ascending cols 11 - 18 descending cols 19 - 20 ascending Thanks (1 Reply)
Discussion started by: don_0110
1 Replies

9. Shell Programming and Scripting

Ascending & Descending order numbers

Dear All, I have below attached file in which i have many nos, i want the last ascending order nos. The brief description is given below. File 315 381 432 315 381 432 315 381 432 315 381 432 315 381 432 (6 Replies)
Discussion started by: pravani1
6 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