473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

sort command from the terminal

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
9 1617
weaknessforcats
9,208 Expert Mod 8TB
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
1,251 Expert 1GB
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
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
1,251 Expert 1GB
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
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
1,251 Expert 1GB
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
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
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
1,251 Expert 1GB
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

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

Similar topics

7
by: CoralineSage | last post by:
Hi there, I know very little about PHP, but I was just curious to know why exactly would some PHP code run fine from the command line but not from the browser? I'm just curious. I was working...
8
by: DJ Craig | last post by:
I use a web hosting service that gives me FTP access to my directory on a Linux server. I use Mac OS X. I write my code in Dreamweaver MX which gives me color-coding, but I get really tired of...
5
by: Good Man | last post by:
Hi there I am trying to execute a custom-built java program on my linux server via PHP. Basically, a user uploads files via PHP, and then the java program performs some action on these files. ...
4
by: parcour | last post by:
Hi. I wonder if anyone could point me to a simple key mapping example for a command-line application. I would like to map the up and down arrow keys in my application to a couple of command...
16
by: John Salerno | last post by:
Here's my new project: I want to write a little script that I can type at the terminal like this: $ scriptname package1 where scriptname is my module name and any subsequent arguments are the...
0
by: octavia | last post by:
Hi everyone. I am having trouble redirecting the standard output from using the sort command on an existing file. When I do the following sort -t- -k 1.1,2.2 -k 3.2,3.3n $file.txt >temp.txt The...
0
pbmods
by: pbmods | last post by:
FIXING NETINFO ERRORS ON THE COMMAND LINE LEVEL: ADVANCED PREREQS: TERMINAL / COMMAND LINE, USERS AND GROUPS Intro Today, while performing routine maintenance, I noticed that I was no...
2
by: Sehboo | last post by:
I call a stored procedure and get the data in dataset then I do this to sort my data myDS.Tables.Select("" , "terminal DESC"); but it doesn't sort. If I put something in filter then it totally...
51
by: Ojas | last post by:
Hi!, I just out of curiosity want to know how top detect the client side application under which the script is getting run. I mean to ask the how to know whether the script is running under...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.