Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date sort - 4 fields 3

Status
Not open for further replies.

arunrr

Programmer
Oct 2, 2009
103
US
Hello,

Looking for the sort command to sort by date using the following input...

Sun Jan 29 2012
Sun Jan 29 2012
Thu Jan 19 2012
Fri Sep 23 2011
Sat Dec 3 2011
Fri Sep 30 2011
Mon Sep 19 2011
Fri Nov 4 2011
Mon Sep 19 2011
Sat Sep 17 2011
Sat Sep 24 2011

Thanks,
AR
Sat Sep 24 2011
 
Try this:

Code:
awk '
        [green]BEGIN[/green] {
                m[[red]"[/red][purple]Jan[/purple][red]"[/red]]=01
                m[[red]"[/red][purple]Feb[/purple][red]"[/red]]=02
                m[[red]"[/red][purple]Mar[/purple][red]"[/red]]=03
                m[[red]"[/red][purple]Apr[/purple][red]"[/red]]=04
                m[[red]"[/red][purple]May[/purple][red]"[/red]]=05
                m[[red]"[/red][purple]Jun[/purple][red]"[/red]]=06
                m[[red]"[/red][purple]Jul[/purple][red]"[/red]]=07
                m[[red]"[/red][purple]Aug[/purple][red]"[/red]]=08
                m[[red]"[/red][purple]Sep[/purple][red]"[/red]]=09
                m[[red]"[/red][purple]Oct[/purple][red]"[/red]]=10
                m[[red]"[/red][purple]Nov[/purple][red]"[/red]]=11
                m[[red]"[/red][purple]Dec[/purple][red]"[/red]]=12
        }
        { [b]printf[/b] [red]"[/red][purple]%04d%02d%02d %s\n[/purple][red]"[/red],[blue]$4[/blue],m[[blue]$2[/blue]],[blue]$3[/blue],[blue]$0[/blue] }
' inputfile | sort -n -k 1,1 | cut -c 10-

It converts the date to a numeric format that is sortable, adds it as a first column, sorts by the column, and then removes that column again.

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Thanks Annihilannic for the input...

I came up with the following, which also converts to numeric and then back to the date format after sort.

cat inputfile | \
while read DD; do echo $(date -d "$DD" +%s); done | \
sort -n | \
while read DD; do echo $(date -d @"$DD" +"%a %b %d %Y"); done \
> outputfile

Thanks again,
Arun

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top