multiple sort


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers multiple sort
# 1  
Old 10-08-2005
multiple sort

hi

i have a file of the following format

123 abc def 2005-09-23 02:04:55
356 abc def 2005-09-23 09:04:55
123 abc def 2005-09-21 19:02:17
234 abc def 2005-09-23 09:04:55
356 abc def 2002-09-23 09:04:55
123 abc def 2005-09-23 09:04:55


i need to sort by the first column and then the combined 4th and 5th column
(4th and 5th column of date format)

i could achieve sorting by first column only i dont know how to proceed with the second requirement

123 abc def 2005-09-23 09:04:55
123 abc def 2005-09-23 02:04:55
123 abc def 2005-09-21 19:02:17

234 abc def 2005-09-23 09:04:55

356 abc def 2005-09-23 09:04:55
356 abc def 2002-09-23 09:04:55



can u please provide me with some pointers
# 2  
Old 10-08-2005
Hi,
u can use the following command:
sort +n <filename> which sorts the file with n+1 field in a file.
ex:lets say u want to sort on second field ,then value of n has to be +1
# 3  
Old 10-08-2005
thanks gaba

that would pretty well work for sort on single fields

and i had already obtained the result sorting on the first field

my question is regarding multiple sorts

and it is on the date field which i had provided

i am not sure how to proceed with.

Thanks Anyway
# 4  
Old 10-08-2005
Maybe you are looking for something like this:
Code:
$ cat test.txt
1 2 3
1 1 5
1 1 4
$ sort -n -k1 -k2 -k3 test.txt
1 1 4
1 1 5
1 2 3

The '-k' option can be used to sort field by field and even by columns within a particular field. Use the '-t' option to specify a seperator if it is something other than whitespace.

Go through the man page of sort on your system anyway.
# 5  
Old 10-08-2005
To sort date fields:
Code:
$ cat test.txt
09/10/2000
09/09/2000
08/10/1999
08/10/2001
$ sort -t'/' -k3 -k2 -k1 test.txt
08/10/1999
09/09/2000
09/10/2000
08/10/2001

Note: all dates are DD/MM/YYYY
# 6  
Old 10-10-2005
thanks blowtorch -> that was a pointer to me

but solved it myself


awk '{print $1}' file | sort | uniq | while read line
do
grep ${line} file | awk '{ if ( $1=='${line}' ) print $4, $5 }' | \
sed -e 's/[-, ]/:/g' | sort -t":" | sed -e 's/:/-/;s/:/-/;s/:/ /' | while read sub
do
grep ${line} file | grep "${sub}" | awk '{ if ( $1=='${line}' ) print }'
done
done


any pointers on optimising it...
# 7  
Old 10-10-2005
try this one ...

Code:
root_mybox:/tmp # cat infile
123 abc def 2005-09-23 02:04:55
356 abc def 2005-09-23 09:04:55
123 abc def 2005-09-21 19:02:17
234 abc def 2005-09-23 09:04:55
356 abc def 2002-09-23 09:04:55
123 abc def 2005-09-23 09:04:55
root_mybox:/tmp # sort -n +0 +3 +4 infile
123 abc def 2005-09-23 02:04:55
123 abc def 2005-09-23 09:04:55
123 abc def 2005-09-21 19:02:17
234 abc def 2005-09-23 09:04:55
356 abc def 2002-09-23 09:04:55
356 abc def 2005-09-23 09:04:55
root_mybox:/tmp #

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Ls -ltr Sort multiple columns

Hi All, I have one requirement, where I need to have output of ls -l command sorted on 1) first on filename 2) last modified time ( descending ) - latest change first. I am not able to figure out how to do it.. Also I dont have a way to change Date display for ls -ltr command.. I am... (1 Reply)
Discussion started by: freakabhi
1 Replies

2. UNIX for Dummies Questions & Answers

Sort with multiple keys

Please suggest a sort command to achieve the below task. Thanks. I want to sort a file considering multiple keys. Sort Keys: Field 2, Field4 and Field6 Input file vqrs,16,zzz,1235,eq,T abcd,11,zzz,1234,pq,F abcd,10,zzz,1235,pq,F lqrs,15,zzz,1235,eq,T pqrs,12,zzz,1234,eq,F... (3 Replies)
Discussion started by: pretty1234
3 Replies

3. Shell Programming and Scripting

sort on multiple columns

hi all, i have a file , having few columns. i wanted to sort it based on 2nd column and then based on 1st column. But i have some problem in first column. first column have characters and numbers, but problem is number of characters are not same in all rows. Few rows have 13 characters and then... (3 Replies)
Discussion started by: deepakiniimt
3 Replies

4. Shell Programming and Scripting

sort on multiple columns

Howdy! Need to sort a large .txt file containing the following, using sort. First based on the 1st column, and then on the 2nd column: Group01.01 1000500 31 0.913 -1.522974494 Group01.01 1001500 16 0.684 -0.967496041 Group01.01 36500 19 0.476 na Group01.02 365500 15 0.400 na... (1 Reply)
Discussion started by: sramirez
1 Replies

5. Shell Programming and Scripting

sort by based on multiple columns

Hi, Is there any way to sort a file in cshell by sort command, sorting it by multiple fields, like to sort it first by the second column and then by the first column. Thanks forhead (1 Reply)
Discussion started by: Takeeshe
1 Replies

6. Shell Programming and Scripting

sort by multiple columns and reformat...

hello.. I have big file and so far I was able to shink it and make smaller with certains values that I need.. vendor1|2000|1 vendor2|1000|1 vendor2|5000|1 vendor2|500|2 vendor3|1000|2 vendor3|500|3 vendor4|500|3 Vendor5|500|1 vendor6|500|3 Vendor7|1000|1 Vendor8|1000|774... (3 Replies)
Discussion started by: abdulaziz
3 Replies

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

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

9. UNIX for Dummies Questions & Answers

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

10. UNIX for Advanced & Expert Users

sort on multiple keys

Hello, Say I have a file with plain text as shown below. Some columns may have multiple words (like "DESC 1", "DESC 1 2", "DESC 1 2 3"). Let's say the file below has 4 columns: 1st(AA), 2nd(BB), 3rd(DESC 1, ...), 4th(CC 1, ...). 1234567890123456789012345678901234567890 AA BB DESC 1... (1 Reply)
Discussion started by: teqmem
1 Replies
Login or Register to Ask a Question