Sorting Date Field with Sort -k :/


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting Date Field with Sort -k :/
# 8  
Old 01-03-2012
@whyte_rhino
There are rules for posting homework of which the O/P will be aware. Otherwise we assume a commercial environment.
This situation with weird sorts is often reached after poor system design.

On re-reading, I see that you know a filename which did not appear in post #1. Either a tutor or a someone working on the same problem?
# 9  
Old 01-03-2012
Quote:
Originally Posted by methyl
@whyte_rhino
There are rules for posting homework of which the O/P will be aware. Otherwise we assume a commercial environment.
This situation with weird sorts is often reached after poor system design.

On re-reading, I see that you know a filename which did not appear in post #1. Either a tutor or a someone working on the same problem?
Methyl, appreciate the help, however I did have that sort the first time round, starting the years first but I still have the same problem; Could it be my translation messing about with the ',' and '/'s?

Also rhyno, I did try what you said before methyl posted an answer;

I swapped the date to the first part of the file, and then used the delimiter as the / to skip apart however I thought it would but no luck.

So the problem I still have is that the -k only does one part of sorting.
So in this example it has only sorted the last part of testData.csv
# 10  
Old 01-03-2012
Quote:
Originally Posted by methyl
@whyte_rhino
There are rules for posting homework of which the O/P will be aware. Otherwise we assume a commercial environment.
This situation with weird sorts is often reached after poor system design.

On re-reading, I see that you know a filename which did not appear in post #1. Either a tutor or a someone working on the same problem?
Yeah, I was working on the same problem (a couple of weeks back now), but it's due in next week so you're probably going to get a lot of traffic soon :P

This particular issue is probably the most difficult aspect of it, too, which is why I was keen not to just give him the answer.

You're not to know, though, and the support on this forum helped me tremendously.
# 11  
Old 01-03-2012
Quote:
Originally Posted by whyte_rhyno
Yeah, I was working on the same problem (a couple of weeks back now), but it's due in next week so you're probably going to get a lot of traffic soon :P

This particular issue is probably the most difficult aspect of it, too, which is why I was keen not to just give him the answer.

You're not to know, though, and the support on this forum helped me tremendously.

Again; to clarify, I am not looking for an answer, but rather why my solution isn't working.
# 12  
Old 01-03-2012
Quote:
Originally Posted by Glitch100
Methyl, appreciate the help, however I did have that sort the first time round, starting the years first but I still have the same problem; Could it be my translation messing about with the ',' and '/'s?

Also rhyno, I did try what you said before methyl posted an answer;

I swapped the date to the first part of the file, and then used the delimiter as the / to skip apart however I thought it would but no luck.

So the problem I still have is that the -k only does one part of sorting.
So in this example it has only sorted the last part of testData.csv
Post your new sort code, might as well have a crack it now you have the answer anyway :P
# 13  
Old 01-03-2012
Quote:
Originally Posted by whyte_rhyno
Post your new sort code, might as well have a crack it now you have the answer anyway :P

I originally did this:

Code:
sort -n -k 2.9 -k 2.5 -k 2 testData.csv


Which is similar to methyl's solution which works off the year first, but the problem I get is it will only sort one part, not all of them.

The original problem persists.
# 14  
Old 01-03-2012
Without seeing the command which went wrong or the matching data or the result ... here is my guess.
If you started with say:
Code:
sort -n -t","  -k3.7 -k3.4 -k3.1 testData.csv

Within the date field dd/mm/yyyy :
k3.7 matches yyyy ... okay so far.
k3.4 matches mm/yyyy ... its going wrong
k3.1 matches dd/mm/yyyy ... its gone very wrong
Thus the file actually gets sorted to dd/mm/yyyy order and the first two sort keys become irrelevant. The problem was overlapping sort keys. The fact that you did not specify a numeric sort was actually irrelevant in this circumstance because the order of a character sort would be the same.


(Though our posts crossed, the problem looks the same to me. We need to show where the key stops as well as where it starts if we do not have a suitable delimiter).

Last edited by methyl; 01-03-2012 at 02:03 PM.. Reason: posts crossed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Sort by date field in AIX

I wanted to sort the below data on 4th field(comma seperator) based on month and date and time on AIX OS. Input data: 3,AJ,30 Jul 06:30,30 Jul 06:30 5,AJ,30 Jul 06:30,30 Jul 06:49 10,AJ,30 Jul 06:30,02 Jan 05:41 4,AJ,30 Jul 06:30,30 Jul 06:36 2,AJ,30 Jul 06:30,28 Jul 06:45 9,AJ,30 Jul... (2 Replies)
Discussion started by: Amit Joshi
2 Replies

2. Linux

How do I format a Date field of a .CSV file with multiple commas in a string field?

I have a .CSV file (file.csv) whose data are all enclosed in double quotes. Sample format of the file is as below: column1,column2,column3,column4,column5,column6, column7, Column8, Column9, Column10 "12","B000QRIGJ4","4432","string with quotes, and with a comma, and colon: in... (3 Replies)
Discussion started by: dhruuv369
3 Replies

3. Shell Programming and Scripting

Sort help: How to sort collected 'file list' by date stamp :

Hi Experts, I have a filelist collected from another server , now want to sort the output using date/time stamp filed. - Filed 6, 7,8 are showing the date/time/stamp. Here is the input: #---------------------------------------------------------------------- -rw------- 1 root ... (3 Replies)
Discussion started by: rveri
3 Replies

4. UNIX for Dummies Questions & Answers

Sort field by date mm/dd/yyyy

Hello Group, I would like to sort the below file by date (first year then month and day) and I used the following command but it does not work sort -n -t"/" -k3 -k1 -k2 "sample original file" 12/28/2009,1.0353 12/31/2009,1.0357 12/30/2009,1.0364 12/29/2009,1.0366 12/24/2009,1.0386... (6 Replies)
Discussion started by: csierra
6 Replies

5. Shell Programming and Scripting

Sorting on two fields time field and number field

Hi, I have a file that has data in it that says 00:01:48.233 1212 00:01:56.233 345 00:09:01.221 5678 00:12:23.321 93444 The file has more line than this but i just wanted to put in a snippet to ask how I would get the highest number with time stamp into another file. So from the above... (2 Replies)
Discussion started by: pat4519
2 Replies

6. Shell Programming and Scripting

Sorting file by a field, and then by another field.

Hi all, Sorry the title is a mess, but did not find a better description at the time. So here is my problem: I have an input file: 8:Mass40s -- 0 48:Mass40s -- 0 67:Mass40s -- 0 86:Mass40s -- 0 105:Mass40s -- 0 9:Mass -- 1 49:Mass -- 86... (5 Replies)
Discussion started by: Alexis Duarte
5 Replies

7. Shell Programming and Scripting

Sort two columns in a field, one of them being a date

Hi, I have a set of columns in a csv file, my first row being an integer and 2nd being a date. I want to first sort it using the first column and then by the second. for e.g. i have , 1234,09/05/2009,hi 5678,01/01/2008,hi 1234,11/03/2006,hello 5678,28/07/2010,hello i tried this... (5 Replies)
Discussion started by: sweta_doshi
5 Replies

8. Shell Programming and Scripting

Sort alpha on 1st field, numerical on 2nd field (sci notation)

I want to sort alphabetically on the first field and sort in descending numerical order on the 2nd field. With a normal "sort -r -n" it does this: abc ||| 5e-05 ||| bla abc ||| 3 ||| ble def ||| 1 ||| abc def ||| 0.2 ||| def As you can see it ignores the fact that 5e-05 is actually 0.00005... (1 Reply)
Discussion started by: FrancoisCN
1 Replies

9. Shell Programming and Scripting

How to Sort files on date field

:cool: Hi all, I have a pecular issue in sorting these files in Solaris environment. All the below files are modified on November 4th, but I want to sort these files as per date column (eg: 01May07_1623 = ddmmmyy_hhmm) Nov 4 18:27 SONYELEC00.GI22973.01May07_1623.gpg Nov 4 18:27... (4 Replies)
Discussion started by: shivaastrogun
4 Replies

10. Shell Programming and Scripting

How to sort a field in a file having date values

Hi All, I am having a pipe delimited file .In this file the 3rd column is having date values.I need to get the min date and max date from that file. I have used cut -d '|' test.dat -f 3|sort -u But it is not sorting the date .How to sort the date column using unix commands Thanks ... (4 Replies)
Discussion started by: risshanth
4 Replies
Login or Register to Ask a Question