Sorting Date Field with Sort -k :/


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting Date Field with Sort -k :/
# 1  
Old 01-03-2012
Sorting Date Field with Sort -k :/

SOLVED :


Last edited by Glitch100; 01-05-2012 at 01:10 PM..
# 2  
Old 01-03-2012
Please post a data sample which contains commas as delimiters. As posted, the delimiter for the data field is a solidus and there are no comma-delimited fields.
# 3  
Old 01-03-2012
I know exactly what assignment this is for so I won't give away too much.

When you're sorting, think about how you're splitting that sort... You're splitting it by the comma, yet you want to sort by date which has a forward-slash delimiting it.

Also, think about what order those details are being placed in birthday.csv it might make things simpler for you...
# 4  
Old 01-03-2012
Code:
mike,0208,19/08/1991
nickle,0182,02/07/1960
...

Like that?

Thank you for hasty reply.

---------- Post updated at 12:15 PM ---------- Previous update was at 12:06 PM ----------

Quote:
Originally Posted by whyte_rhyno
I know exactly what assignment this is for so I won't give away too much.

When you're sorting, think about how you're splitting that sort... You're splitting it by the comma, yet you want to sort by date which has a forward-slash delimiting it.

Also, think about what order those details are being placed in birthday.csv it might make things simpler for you...
As far as I am aware the / has nothing to do with it as I am using the -k wildcard which does it by position.
:/
# 5  
Old 01-03-2012
Quote:
Originally Posted by Glitch100
Code:
mike,0208,19/08/1991
nickle,0182,02/07/1960
...

Like that?

Thank you for hasty reply.

---------- Post updated at 12:15 PM ---------- Previous update was at 12:06 PM ----------



As far as I am aware the / has nothing to do with it as I am using the -k wildcard which does it by position.
:/
As I said, I know this is an assignment, so if I just give you the answer, we'll both get into trouble and you won't learn anything. I can see you're actually trying (unlike most of our peers), but the fact remains that this is an assignment meant to test our abilities.

Look at what sort has to offer. -n offers a numerical sort (great for dates which have been chopped up...)

As I said previously to you, this is a huge hint now, make things easier for yourself and THINK about the order of your .csv file. Who said those details had to go in that order you've specified...

Yes, you will still need -k, too. But think about what it does.


That's all I'm offering. :P
# 6  
Old 01-03-2012
Quote:
sort -n -t"," -k3.1 -k3.4 -k3.7 testData.csv
You were pretty close. In the comma-separated file the date is indeed field number 3.


However to sort to date order we need to sort to Year-Month-Day order (not Day-Month-Year order). Because we don't have a consistent delimiter we are reduced to specifying character positions within field 3.
Code:
sort -t',' -k3.7n,3.10n -k3.4n,3.5n -k3.1n,3.2n testData.csv

There may be much more to do depending on how you want to deal with duplicate dates. Think about the order of fields 1 and 2 in that situation.

This is also an exercise in data file design. You can make life so much easier by storing dates in an easy-to-sort and easy-to-match format.
# 7  
Old 01-03-2012
Or, you know, one of the mods just gifts you the answer Smilie

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