Sorting Date Field with Sort -k :/


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting Date Field with Sort -k :/
# 15  
Old 01-03-2012
Quote:
Originally Posted by Glitch100
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.
Let's think methodically what we want to do here:

I'm assuming I have this data in the csv in this order:

11/12/2001,1234,Paul Stephens

So, I want to sort by number

Code:
sort -n

then I want split that date, thankfully the date has a nice delimiter to use:

Code:
sort -n  -t"/"

So now I can sort the date easily, but as this date is dd/mm/yyyy I need to reverse it to make the sort work properly:

Code:
sort -n  -t"/" -k3 -k2 -k1 testData.csv

You might want to use a function that -o(utputs) to a file...
# 16  
Old 01-03-2012
Quote:
Originally Posted by methyl
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.

sort -t',' -k3.7n,3.10n -k3.4n,3.5n -k3.1n,3.2n testData.csv

Yea I understand, and that's why yours specifies the exact locations:
Yet, it still fails... :'( ... It's really getting on my nerves, but I appreciate your constant support.

---------- Post updated at 01:05 PM ---------- Previous update was at 01:03 PM ----------

Quote:
Originally Posted by whyte_rhyno
Let's think methodically what we want to do here:

I'm assuming I have this data in the csv in this order:

11/12/2001,1234,Paul Stephens

So, I want to sort by number

Code:
sort -n

then I want split that date, thankfully the date has a nice delimiter to use:

Code:
sort -n  -t"/"

So now I can sort the date easily, but as this date is dd/mm/yyyy I need to reverse it to make the sort work properly:

Code:
sort -n  -t"/" -k3 -k2 -k1 testData.csv

You might want to use a function that -o(utputs) to a file...
In my above post I said I tried that; but it still failed; which is why I am convinced there is something wrong with my client and the ,'s and the /'s .

I am not looking for an answer, I already had one, but something's going wrong. I just can't see it.


_______________________________
_____________________________

EDIT:

Tried various variations of code:

And with this for example:

Code:
sort -t',' -k3.7n,3.10n -k3.4n,3.5n -k3.1n,3.2n birthday.csv

Minas,284729,14/05/1956
Bono,92374,08/07/1970
Glads,02883,07/01/1987
Mario,09281,02/17/1987
Boomer,0182,18/11/1988
Jesus,01322,17/03/1991


That's the output: As you can see sorted in third column, not others :'(

Last edited by Glitch100; 01-03-2012 at 03:10 PM..
# 17  
Old 01-03-2012
Code:
sort -t',' -k3.7n,3.10n -k3.4n,3.5n -k3.1n,3.2n birthday.csv

Minas,284729,14/05/1956
Bono,92374,08/07/1970
Glads,02883,07/01/1987
Mario,09281,02/17/1987
Boomer,0182,18/11/1988
Jesus,01322,17/03/1991

Imho. I can't see anything wrong with this output. Assuming the test data is meant to be dd/mm/yyyy (despite month "17" in the middle column!), the data is in the correct sorted order - ascending yyyy/mm/dd.
Try a bigger data file with a decent variation of dates (I did). Your test data only has one case where the dates are in different years.
This User Gave Thanks to methyl For This Post:
# 18  
Old 01-05-2012
Ok Methyl, thank you for your help; that last post was most helpful as you actually pointed out to me that I was looking at it all wrong; stupid of me, but I was somehow expecting it to sort by date, and then move all the months in order and dates, which is impossible without altering. So thanks for pointing it out, one of the first sets of code I used worked, so thanks for pointing out something staring in my face.
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