By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

sort command from the terminal

P: 6
Hello! I'm having huge difficulties with trying to sort the file below. I would like to be able to sort the third field(with delimiter '- ') so that it runs from O1 - O13. i can't seem to bypass the fact that 10 goes before 1. I am using the "sort" command from the terminal. Any help would be much appreciated thanks!

1A-BL-O10 2005038-08 TXslot=20 Fibre:21 2005038-06 RXslot=20 Fibres:42 43
1A-BL-O11 2005038-08 TXslot=20 Fibre:20 2005038-06 RXslot=20 Fibres:40 41
1A-BL-O1 2005038-08 TXslot=20 Fibre:17 2005038-07 RXslot=20 Fibres:34 35
1A-BL-O12 2005038-08 TXslot=20 Fibre:19 2005038-06 RXslot=20 Fibres:38 39
1A-BL-O13 2005038-08 TXslot=20 Fibre:18 2005038-06 RXslot=20 Fibres:36 37
1A-BL-O2 2005038-08 TXslot=20 Fibre:16 2005038-07 RXslot=20 Fibres:32 33
1A-BL-O3 2005038-08 TXslot=20 Fibre:15 2005038-07 RXslot=20 Fibres:30 31
1A-BL-O4 2005038-08 TXslot=20 Fibre:14 2005038-07 RXslot=20 Fibres:28 29
1A-BL-O5 2005038-08 TXslot=20 Fibre:13 2005038-07 RXslot=20 Fibres:26 27
1A-BL-O6 2005038-08 TXslot=20 Fibre:12 2005038-07 RXslot=20 Fibres:24 25
1A-BL-O7 2005050-08 TXslot=20 Fibre:0 2005050-07 RXslot=20 Fibres:0 1
1A-BL-O8 2005038-08 TXslot=20 Fibre:23 2005038-06 RXslot=20 Fibres:46 47
1A-BL-O9 2005038-08 TXslot=20 Fibre:22 2005038-06 RXslot=20 Fibres:44 45
Jun 5 '07 #1
Share this Question
Share on Google+
9 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
Most sorts allow you to provide a function pointer to your own comparator function.

So you write a function where 10 comes after 9 and pass the address of this function to the sort.

Check the sort function prototype for the exact type of function pointer required.

In the case of the C++ STL sort, the comparator function must be a binary predicate. That is, a function with two arguments of the type being sorted that returns a bool. The function should return true of the first argument is less than the second argument. Since this is your function, you can decide what "less" means.
Jun 5 '07 #2

AdrianH
Expert 100+
P: 1,251
Hello! I'm having huge difficulties with trying to sort the file below. I would like to be able to sort the third field(with delimiter '- ') so that it runs from O1 - O13. i can't seem to bypass the fact that 10 goes before 1. I am using the "sort" command from the terminal. Any help would be much appreciated thanks!

1A-BL-O10 2005038-08 TXslot=20 Fibre:21 2005038-06 RXslot=20 Fibres:42 43
1A-BL-O11 2005038-08 TXslot=20 Fibre:20 2005038-06 RXslot=20 Fibres:40 41
1A-BL-O1 2005038-08 TXslot=20 Fibre:17 2005038-07 RXslot=20 Fibres:34 35
1A-BL-O12 2005038-08 TXslot=20 Fibre:19 2005038-06 RXslot=20 Fibres:38 39
1A-BL-O13 2005038-08 TXslot=20 Fibre:18 2005038-06 RXslot=20 Fibres:36 37
1A-BL-O2 2005038-08 TXslot=20 Fibre:16 2005038-07 RXslot=20 Fibres:32 33
1A-BL-O3 2005038-08 TXslot=20 Fibre:15 2005038-07 RXslot=20 Fibres:30 31
1A-BL-O4 2005038-08 TXslot=20 Fibre:14 2005038-07 RXslot=20 Fibres:28 29
1A-BL-O5 2005038-08 TXslot=20 Fibre:13 2005038-07 RXslot=20 Fibres:26 27
1A-BL-O6 2005038-08 TXslot=20 Fibre:12 2005038-07 RXslot=20 Fibres:24 25
1A-BL-O7 2005050-08 TXslot=20 Fibre:0 2005050-07 RXslot=20 Fibres:0 1
1A-BL-O8 2005038-08 TXslot=20 Fibre:23 2005038-06 RXslot=20 Fibres:46 47
1A-BL-O9 2005038-08 TXslot=20 Fibre:22 2005038-06 RXslot=20 Fibres:44 45
Try:

sort -g -t- -k3.2

The -k states what key. 3 is the third field, .2 means start from the second char of that field. -g states to use numerical sort.

If you don't want to use - as your field seperator, use

sort -g -k1.8

since the number is always starting at the same location in the first field.



Hmmm. Looks like we should start up a shell forum. I could lead that. ;)


Adrian
Jun 5 '07 #3

P: 6
Hi Adrian,

The problem is that I need the same order to remain for 1A and BL etc. Does that make any sense? The output is to be the same as I showed but with the 1A-BL-O10 to appear in the right place!

I think I'm going mad! A script forum sounds like a great idea!
Jun 5 '07 #4

AdrianH
Expert 100+
P: 1,251
Hi Adrian,

The problem is that I need the same order to remain for 1A and BL etc. Does that make any sense? The output is to be the same as I showed but with the 1A-BL-O10 to appear in the right place!

I think I'm going mad! A script forum sounds like a great idea!
The sort command doesn't erase anything from a record(line), it just reorders it. Using the switches on the text you provided worked without problem. If you test it and it is not correct, let me know.


Adrian
Jun 6 '07 #5

P: 6
Hi Adrian. Sorry, I didn't explain myself very well. Your command line worked but the problem is that the file I gave you is only part of the file I want to manipulate. I the file where it says BL (bottom left) I have also a corresponding TR, TL, BR. Then when I run the command on that file, the order of the quadrants becomes disordered. So all the numbers are sorted properly but the quadrants get mixed up.

Any ideas?
Jun 6 '07 #6

AdrianH
Expert 100+
P: 1,251
Hi Adrian. Sorry, I didn't explain myself very well. Your command line worked but the problem is that the file I gave you is only part of the file I want to manipulate. I the file where it says BL (bottom left) I have also a corresponding TR, TL, BR. Then when I run the command on that file, the order of the quadrants becomes disordered. So all the numbers are sorted properly but the quadrants get mixed up.

Any ideas?
Oh, then sort the sorted using a - as your delimiter on field 2.


Adrian
Jun 6 '07 #7

P: 6
Oh, then sort the sorted using a - as your delimiter on field 2.


Adrian
Hey.. No that doesn't seem to work. The quadrants stay grouped together which is good but the numerical order still has O13 preceding O2.
Jun 6 '07 #8

P: 6
Hey.. No that doesn't seem to work. The quadrants stay grouped together which is good but the numerical order still has O13 preceding O2.
Hey Peeps, I got it running, check this out:

sort -t- -k 1.1,2.2 -k 3.2,3.3n file.txt

Thanks a lot for all your input!

Octavia
Jun 6 '07 #9

AdrianH
Expert 100+
P: 1,251
Hey Peeps, I got it running, check this out:

sort -t- -k 1.1,2.2 -k 3.2,3.3n file.txt

Thanks a lot for all your input!

Octavia
That's interesting, didn't know you could have a secondary sort. I forgot to tell you to sort the sorted you probably need to stabilise it by using switch -s, but your way is better I think.


Adrian
Jun 6 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.