Date Sorting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date Sorting
# 8  
Old 05-25-2007
Quote:
Originally Posted by LiquidChild
This does not seem to work in the following though

FILE_230580.ok
FILE_230590.ok
FILE_010107.ok

As the ordering is then:

FILE_010107.ok
FILE_230580.ok
FILE_230590.ok
There are two problems :
- These files seems to be in ddmmyy format.
- The yy format is not sortable, because with this format 2007 is lower than 1980

In that case you can do something like that:
Code:
$ ls File_*
FILE_010107.ok  FILE_230580.ok  FILE_230590.ok
$ ls FILE_* | \
> awk -F_ \
>   '{
>      dd=substr($2,1,2);
>      mm=substr($2,3,2);
>      yy=substr($2,5,2);
>      print (yy<70 ? "20" : "19") yy mm dd, $0 ;
>    }' | \
> sort -k1,1 | \
> cut -d' ' -f2
FILE_230580.ok
FILE_230590.ok
FILE_010107.ok$

Jean-Pierre.
# 9  
Old 05-29-2007
What if the only yy would be above 2000, so 07, 08, 09 etc, would it make the solution simplier? If being honest its unlikely we will have a file pre 2000.
# 10  
Old 05-29-2007
Quote:
Originally Posted by LiquidChild
What if the only yy would be above 2000, so 07, 08, 09 etc, would it make the solution simplier? If being honest its unlikely we will have a file pre 2000.
Code:
ls FILE* | sort -t_ -k2.5,2.6 -k2.3,2.4 -k2.1,2.2

Jean-Pierre.

Last edited by aigles; 05-29-2007 at 08:16 AM.. Reason: Correction of typing error
# 11  
Old 05-29-2007
Thanks Jean, will give that a go, I tried using multiple keys, but have not seen the '.' notation before, what does that mean?
# 12  
Old 05-29-2007
From sort man pages :
Quote:
The -k KeyDefinition flag uses the following form:

-k [ FStart [ .CStart ] ] [ Modifier ] [ , [ FEnd [ .CEnd ] ][ Modifier ] ]

The sort key includes all characters beginning with the field specified by the
FStart variable and the column specified by the CStart variable and ending with
the field specified by the FEnd variable and the column specified by the CEnd
variable. If Fend is not specified, the last character of the line is assumed.
If CEnd is not specified the last character in the FEnd field is assumed. Any
field or column number in the KeyDefinition variable may be omitted. The default
values are:

FStart Beginning of the line

CStart First column in the field

FEnd End of the line

CEnd Last column of the field

If there is any spaces between the fields, sort considers them as separate
fields.

The value of the Modifier variable can be one or more of the letters b, d, f, i,
n, or r. The modifiers apply only to the field definition they are attached to
and have the same effect as the flag of the same letter. The modifier letter b
applies only to the end of the field definition to which it is attached. For
example:
-k 3.2b,3r


specifies a sort key beginning in the second nonblank column of the third field
and extending to the end of the third field, with the sort on this key to be
done in reverse collation order. If the FStart variable and the CStart variable
fall beyond the end of the line or after the FEnd variable and the CEnd
variable, then the sort key is ignored.
Jean-Pierre.

Last edited by aigles; 05-29-2007 at 06:24 AM..
# 13  
Old 05-29-2007
Thanks for the all the help Jean-Pierre.

I have read the above but am not clear on one thing, I get the .5 .6 which basically equates to the last two fields of the string, i.e. the year, and sorts on them first and the same for .3 .4 and .1 .2, but I don't really understand the 2. and 3. part. While I know this refers to the field and the part after the . refers to the character within the field, I don't understand how it can be 2.5 and 2.6, how are fields split up in the string, i.e. what exactly is field 2 and field 3.

Sorry I did read the above and have had a look for some further text and examples and while a bit clearer I still don't fully understand it
# 14  
Old 05-29-2007
I have corrected typing errors :
Code:
ls FILE* | sort -t_ -k2.5,2.6 -k2.3,2.4 -k2.1,2.2

With '_' as field separator, the date is field 2 is in the form ddmmyy.ok
-k2.5,2.6 : Chars 5 to 6 of field 2 -> yy
-k2.3,2.4 : Chars 3 to 4 of field 2 -> mm
-k2.1,2.2 : Chars 1 to 2 of field 2 -> dd

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Sorting on fields for last date

Hi all, I have a file with a list of rpm's that have different dates. I am trying to just grab the latest rpm and install date, and discard the rest. The file has 1000's of entries all with different names and dates. I have tried sort -k on the file and I am not grabbing the info, ... (4 Replies)
Discussion started by: gartie
4 Replies

2. Shell Programming and Scripting

Sorting on date basis

I have file data.txt having below data cat data.txt 01-MAY-13 2.38.11.00.100089 IN 4512 0000741881 01-MAY-13 2.38.11.00.100089 IN 4512 0000741881 01-JUN-13 2.38.11.00.100089 FC 1514 0000764631 01-NOV-13 2.38.11.00.100089 FC 1514 0000856571 01-NOV-13 2.38.11.00.100089 IN 300.32... (1 Reply)
Discussion started by: ranabhavish
1 Replies

3. Shell Programming and Scripting

Sorting by date

I am trying to sort by two columns. The first column in an ID, the second is a date in the form yyyy-mm-dd. I need to sort by the ID column, then in ascending order for the date column (earliest date to most recent date compared to today). Input data: 012-abc 2012-04-25 ... (3 Replies)
Discussion started by: mollydog11
3 Replies

4. Shell Programming and Scripting

Sorting the data with date

Hi, PFB the data: C_Random_130417 Java_Random_130518 Perl_Random_120519 Perl_Random_120528 so the values are ending with year,i.e.,130417 i want to sort the values with date. i want the output like this: Perl_Random_120519 Perl_Random_120528 C_Random_130417 Java_Random_130518 can... (5 Replies)
Discussion started by: arindam guha
5 Replies

5. Shell Programming and Scripting

date(ddmmyyyy) sorting

input : 20110730 20110730 20110731 20110731 20110801 20110801 20110801 20110813 20110815 01062011 01062011 OUTPUT : i need to sort this input in such a way so that the latest date comes first. (11 Replies)
Discussion started by: urfrnddpk
11 Replies

6. Shell Programming and Scripting

Date and Time sorting

Hi Guys! i have a problem of sorting column chronologically because the data i have in column is in the following format 06/Dec/2006:18:09:54 and need to be sorted in the following way (upto seconds) 06/Dec/2005:18:09:50 06/Dec/2005:18:09:51 31/Mar/2006:19:30:41 24/Oct/2006:19:16:19... (4 Replies)
Discussion started by: me_newbie
4 Replies

7. UNIX for Dummies Questions & Answers

Sorting by date and time

Hi guys... I've been trying to do this for ages. Maybe you can help. I have log files like the examples below and I have grepped out certain lines from the files so that I can get an idea of who is logging on and how. So now I have the information in a new file but it is now in a different order... (7 Replies)
Discussion started by: padmundo
7 Replies

8. Shell Programming and Scripting

Sorting a date coulumn

Hi All, I have a file say abc.txt with the below data. 1234 876S 01Mar2007 foo 1244 65DF 19Jan2007 boo 9924 234K 01Jan2006 koo 8866 8FGH 12Feb1999 roo 7777 ASDF 13May2007 soo I need this file to be in sorted order depending on the date field. e.g 8866 8FGH 12Feb1999 roo... (2 Replies)
Discussion started by: rinku11
2 Replies

9. UNIX for Dummies Questions & Answers

date sorting

Hi at all, I have to sort a log file on timestamp field. That's field is the third! a log file sample..... 1|EVTVOD-1-20060709_000614|2006/07/09-0:11:23|0.3.8 1|EVTVOD-1-20060709_000614|2006/07/09-0:11:16|0.3.8 1|EVTVOD-1-20060709_000614|2006/07/09-0:11:20|0.3.8... (3 Replies)
Discussion started by: nmilella
3 Replies

10. UNIX for Dummies Questions & Answers

sorting on date

I have a file where dates in the form mm/dd/yyyy is the first field. How do I sort the file by the date field? Thanks, Duckman (6 Replies)
Discussion started by: Duckman
6 Replies
Login or Register to Ask a Question