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
9924 234K 01Jan2006 koo
1244 65DF 19Jan2007 boo
1234 876S 01Mar2007 foo
7777 ASDF 13May2007 soo

Any help is appreciated.

nawk -f rinku.awk abc.txt | sort -n | cut -d' ' -f2-

rinku.awk

BEGIN {
      monthA="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
      monN=split(monthA, monA, FS)
      # invert a monA array to be indexed by the NAME of the month
      for(i=1; i<=monN; i++) {
        monA[monA]=i
        delete monA
      }
}
{
   _day=substr($3,1,2)
   _mon=substr($3,3,3)
   _year=substr($3,6)
   printf("%4d%02d%02d%s%s\n", _year, monA[_mon], _day, OFS, $0)
}

Or all in ksh:

#!/bin/ksh

monthList='JanFebMarAprMayJunJulAugSepOctNovDec'

month2number() # $1 = month
{
    typeset monthName="${1}"
    typeset idx;
    idx=${monthList%%${monthName}*}
    printf "%02d" "$(( (${#idx} + 3 ) / 3 ))"
}

while read one two date four junk
do
    _day=$(echo ${date} | sed 's/^\(..\).*/\1/')
    _mon=$(echo ${date} | sed 's/^..\(...\).*/\1/')
    _year=$(echo ${date} | sed 's/.*\(....\)/\1/')
    echo "${_year}$(month2number ${_mon})${_day} " " $one " " $two " " $date " " $four"
done < abc.txt | sort -n | cut -d' ' -f2-

you can use sort, if your sort version has the -M option.