Sorting dates in chronological order


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting dates in chronological order
# 8  
Old 07-14-2011
Assuming that the dates in the 2nd column do not repeat in the file, you could do something like this in Perl -

Code:
$
$ # show the content of the data file "f7"
$
$ cat -n f7
     1  H|20-May-2011|MF_FF.dat|77164|731374590.96|1|1|731374590.96|76586|77164|578|2988|Y
     2  H|21-Jul-2011|MF_FF.dat|77178|731374590.96|2|4|731374590.96|76600|77178|578|3002|Y
     3  H|21-May-2011|MF_FF.dat|77178|731374590.96|2|4|731374590.96|76600|77178|578|3002|Y
     4  H|22-Aug-2011|MF_FF.dat|77195|731374590.96|3|4|731374590.96|76617|77195|578|3019|Y
     5  H|22-May-2011|MF_FF.dat|77195|731374590.96|3|4|731374590.96|76617|77195|578|3019|Y
     6  H|26-Mar-2011|MF_FF.dat|76444|713606875.00|1|1|713606875.00|75822|76444|622|3310|Y
     7  H|31-Oct-2009|MF_FF.dat|76444|713606875.00|1|1|713606875.00|75822|76444|622|3310|Y
     8  H|07-Dec-2010|MF_FF.dat|76444|713606875.00|1|1|713606875.00|75822|76444|622|3310|Y
$
$ # run the Perl program on the data file "f7"
$
$ perl -F"\|" -lane 'BEGIN {%x = qw(Jan 01 Feb 02 Mar 03 Apr 04 May 05 Jun 06
                                   Jul 07 Aug 08 Sep 09 Oct 10 Nov 11 Dec 12)}
                    ($d,$m,$y) = unpack("A2xA3xA4", $F[1]);
                    $data{$y.$x{$m}.$d} = $_;
                    END {foreach $k (sort keys %data) {print $data{$k}}}
                   ' f7
H|31-Oct-2009|MF_FF.dat|76444|713606875.00|1|1|713606875.00|75822|76444|622|3310|Y
H|07-Dec-2010|MF_FF.dat|76444|713606875.00|1|1|713606875.00|75822|76444|622|3310|Y
H|26-Mar-2011|MF_FF.dat|76444|713606875.00|1|1|713606875.00|75822|76444|622|3310|Y
H|20-May-2011|MF_FF.dat|77164|731374590.96|1|1|731374590.96|76586|77164|578|2988|Y
H|21-May-2011|MF_FF.dat|77178|731374590.96|2|4|731374590.96|76600|77178|578|3002|Y
H|22-May-2011|MF_FF.dat|77195|731374590.96|3|4|731374590.96|76617|77195|578|3019|Y
H|21-Jul-2011|MF_FF.dat|77178|731374590.96|2|4|731374590.96|76600|77178|578|3002|Y
H|22-Aug-2011|MF_FF.dat|77195|731374590.96|3|4|731374590.96|76617|77195|578|3019|Y
$
$

tyler_durden
# 9  
Old 07-15-2011
And here's the awk version...
Code:
awk -F\| '{
    n=split($2,a,"-")
    for (i=n; i; i--) {
        if (a[i]=="Jan")      a[i]="01"
        else if (a[i]=="Feb") a[i]="02"
        else if (a[i]=="Mar") a[i]="03"
        else if (a[i]=="Apr") a[i]="04"
        else if (a[i]=="May") a[i]="05"
        else if (a[i]=="Jun") a[i]="06"
        else if (a[i]=="Jul") a[i]="07"
        else if (a[i]=="Aug") a[i]="08"
        else if (a[i]=="Sep") a[i]="09"
        else if (a[i]=="Oct") a[i]="10"
        else if (a[i]=="Nov") a[i]="11"
        else if (a[i]=="Dec") a[i]="12"
        s = sprintf("%s", a[i]?s""a[i]:a[i])
    }
    if (s in c) x[s]=sprintf("%s",x[s]"\n"$0)
    else {c[s] = s; b[NR] = s; x[s] = $0}
    s=""
} END {
    for (i=1; i<NR; i++)
        for (j=1; j<=NR-i; j++)
            if (b[j] > b[j+1]) {
            t = b[j]
            b[j] = b[j+1]
            b[j+1] = t
        }
    for (i=1; i<=NR; i++)
        if (x[b[i]]) print x[b[i]]
}' file

This User Gave Thanks to shamrock For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sort in chronological order

I am trying to sort a log file in chronological order to identify which ones did not process and still have an old (probably yesterday's) date. This is a sample of the file:flatf 010140 flatf Thu May 10 22:22:11 CST 2018 flats finished flatf 010142 flatf Thu May 10 22:31:25 CST 2018 flats... (4 Replies)
Discussion started by: wbport
4 Replies

2. Shell Programming and Scripting

Sorting strings in reverse order

Hello, I have a large database of words and would like them sorted in reverse order i.e. from the end up. An example will make this clear: I have tried to write a program in Perl which basically takes the string from the end and tries to sort from that end but it does not seem... (5 Replies)
Discussion started by: gimley
5 Replies

3. UNIX for Dummies Questions & Answers

sorting sequences in ascending order

Hi, I have this single file with a number of sequence inside it of format >string1 data >string100 data >string10 ..... >string5 ... >string67 ...... the dots represent data. I wanted to get the sequences arranged in ascending order like >string1 data >string5 (5 Replies)
Discussion started by: sonia102
5 Replies

4. UNIX for Dummies Questions & Answers

Any way to get find command o/p in chronological order?

Hi friends, I am using below script to gzip files after naming them in a particular order. but I intend to name them in numerical order as per their timings(earlier updated fle with a smaller numeric extension than later updated),but this script is not working as planned. please help with... (7 Replies)
Discussion started by: Jcpratap
7 Replies

5. Shell Programming and Scripting

Sorting dates in Perl

I have a directory of backup files. named like this: ldap.data.04-06-2012.tar ldap.data.03-06-2012.tar ldap.data.02-06-2012.tar ldap.data.01-06-2012.tar ldap.data.31-05-2012.tar ldap.data.30-05-2012.tar ldap.data.29-05-2012.tar ldap.data.28-05-2012.tar ldap.data.27-05-2012.tar... (6 Replies)
Discussion started by: robsonde
6 Replies

6. UNIX for Dummies Questions & Answers

sorting PID in decreasing order using ps?

I am trying to sort the output in decreasing order of the process ID while using the ps command. I am having trouble doing this using the --sort part of ps. Also I was wondering if anyone knows what the "S" stands for under the process's status code? (3 Replies)
Discussion started by: crimputt
3 Replies

7. Shell Programming and Scripting

multiple sorting with different order

Hi Guys, I have data like this HOS05 23/12/2008 10AM HOS06 15/12/2008 2PM HOS62 29/12/2008 10AM HOS64 23/12/2008 2PM HOS70 26/12/2008 10AM ZFT01 06/12/2008 10AM HOS73 11/12/2008 2PM MHOS0 05/12/2008 10AM MHOS0 20/12/2008 2PM MHOS0 27/12/2010 2PM MHOS0 11/12/2008 10AM MHOS0 30/12/2009... (1 Reply)
Discussion started by: ckarunprakash
1 Replies

8. UNIX for Advanced & Expert Users

Uploading files in chronological order

Thanks for your help. (3 Replies)
Discussion started by: circuit.muni
3 Replies

9. UNIX for Dummies Questions & Answers

Order sorting

How do you sort text in order using sed? :confused: For example 01 B D A C to 01 ABCD (3 Replies)
Discussion started by: evoGage
3 Replies

10. UNIX for Advanced & Expert Users

Sorting filenames by order in another file

I would like to arrange /sort filenames ending with suffix like ".00XXXX". where X is a digit. However the order of arrangement is in a text file and is 'harpharzard'. e.g the text file may be like 002345 009807 001145 I wanted to avoid doing this using sql and exporting the text file back to... (4 Replies)
Discussion started by: samudimu
4 Replies
Login or Register to Ask a Question