sort file text by date as a "ls -t"

I have, a file wich list file on server ftp and i want to sort the file text as the command "ls -t"

i.e

drwxr-x---  2 ftp_mont System            0 Sep 30 09:16 .
drwxr-x---  2 ftp_mont System            0 Jul 15  2009 ..
-rwxr-x---  1 ftp_mont System    343998791 Sep 01 09:20 manif laitiers-H264.mov
-rwxr-x---  1 ftp_mont System     12063097 Sep 27 15:32 Mende_Mende_ITW.mov
-rwxr-x---  1 ftp_mont System       918234 Jun 27 15:44 Mende_Mende_COMJUN.m4a
-rwxr-x---  1 ftp_mont System       918234 Jan 22 15:44 Mende_Mende_COMJAN.m4a
-rwxr-x---  1 ftp_mont System       918234 Jan 20 15:44 Mende_Mende_COMJUN.m4a
-rwxr-x---  1 ftp_mont System       918234 Jan 22 16:44 Mende_Mende_COMJUN.m4a
-rwxr-x---  1 ftp_mont System       918234 Oct 27 15:44 Mende_Mende_COMOCT.m4a
-rwxr-x---  1 ftp_mont System       918234 Sep 27 15:44 Mende_Mende_COM.m4a
-rwxr-x---  1 ftp_mont System    250324761 Sep 27 15:47 Dossier_secheresse-H264-1.mov
-rwxr-x---  1 ftp_mont System    169097455 Sep 27 15:55 Mende_Mende-H264.mov
-rwxr-x---  1 ftp_mont System    343998791 Sep 30 09:20 manif laitiers-H264.mov
-rwxr-x---  1 ftp_mont System    343998791 Sep 30 10:20 manif laitiers1-H264.mov
-rwxr-x---  1 ftp_mont System    343998791 Sep 31 09:20 manif laitiers2-H264.mov

I have imagined this code, but i think there are much more simple code...

cat /Users/macbook/Desktop/test | sed '1,2'd | sort -t : -k 1,2 | sort -t : -k 2,2 | sort -k 7 |  awk '{if($6=="Jan")a[NR]=$0}
{if($6=="Feb")b[NR]=$0}
{if($6=="Mar")c[NR]=$0}
{if($6=="Apr")d[NR]=$0}
{if($6=="May")e[NR]=$0}
{if($6=="Jun")f[NR]=$0}
{if($6=="Jul")g[NR]=$0}
{if($6=="Aug")h[NR]=$0}
{if($6=="Sep")j[NR]=$0}
{if($6=="Oct")k[NR]=$0}
{if($6=="Nov")l[NR]=$0}
{if($6=="Dec")m[NR]=$0}
END{
for(i=1;i<=length(a);i++){print a};
for(i=1;i<=length(b);i++){print b};
for(i=1;i<=length(c);i++){print c};
for(i=1;i<=length(d);i++){print d};
for(i=1;i<=length(e);i++){print e};
for(i=1;i<=length(f);i++){print f};
for(i=1;i<=length(g);i++){print g};
for(i=1;i<=length(h);i++){print h};
for(i=1;i<=length(j);i++){print j};
for(i=1;i<=length(k);i++){print k};
for(i=1;i<=length(l);i++){print l};
for(i=1;i<=length(m);i++){print m}
}' | awk 'NF{print $9,$10,$11,$12,$13,$14,$15}' | awk '{a[NR]=$0}END{for(i=FNR;i>=0;i--){print a}}'

Thanx.

The output of "ls -l" is not uniform in that it displays time differently (including years) depending on whether the file is older than 6 months or not. Even they are all younger than 6 month, you have to take into consideration that December is older than February when the current date is January. Not sure if the output of ls from ftp behave the same way.

awk '!/^d/ {
  for (i=6; i<=NF; i++)
    printf "%s", $i (i == NF ? RS : FS)
  }' infile | 
      sort -Mr |
        cut -d\  -f4-
#!/bin/sh
grep '^-' infile|while read line; do
  echo "$line"'|'$(date -d "$(echo $line|awk '{print $6 " " $7 " " $8}')" '+%s')
done|sort -t'|' -k2nr|cut -f1 -d'|'

Not very elegant but it works, using GNU date to make sense of the different dates with year/month/day or month/day/time (IMO sort -M is not accurate in this case as it does not take into account year dates).

thank you for your answer scrutinizer but i'm on mac os X and the options of date don't work...

Bummer. Then perhaps you could use a shell function to convert to epoch time for sorting. E.g. like so:

#!/bin/sh
convertdate() {
  cdate="$6 $7 $8"
  case $cdate in
    *[1-9][0-9][0-9][0-9])
        informat='%b %d %Y';;
    *)  informat='%b %d %T' ;;
  esac
  date -j -f "$informat" "$cdate" "+%s"
}

grep '^-' infile|while read line; do
  echo "$line"'|'$(convertdate $line)
done|sort -t'|' -k2rn|cut -f1 -d'|'

You'd have to test it yourself I do not have Mac OS X (yet ;))

No, unfortunately, it's don't work....i keep my code for the moment.
I 'll study the command date for mac os X

thanks for all.

Could you post the output you get from the pipeline I posted?

Output is:

-rwxr-x---  1 ftp_mont System       918234 Oct 27 15:44 Mende_Mende_COMOCT.m4a
-rwxr-x---  1 ftp_mont System    343998791 Sep 31 09:20 manif laitiers2-H264.mov
-rwxr-x---  1 ftp_mont System    343998791 Sep 30 10:20 manif laitiers1-H264.mov
-rwxr-x---  1 ftp_mont System    343998791 Sep 30 09:20 manif laitiers-H264.mov
-rwxr-x---  1 ftp_mont System    169097455 Sep 27 15:55 Mende_Mende-H264.mov
-rwxr-x---  1 ftp_mont System    250324761 Sep 27 15:47 Dossier_secheresse-H264-1.mov
-rwxr-x---  1 ftp_mont System       918234 Sep 27 15:44 Mende_Mende_COM.m4a
-rwxr-x---  1 ftp_mont System       918234 Jun 27 15:44 Mende_Mende_COMJUN.m4a
-rwxr-x---  1 ftp_mont System       918234 Jan 22 16:44 Mende_Mende_COMJUN.m4a
-rwxr-x---  1 ftp_mont System       918234 Jan 22 15:44 Mende_Mende_COMJAN.m4a
-rwxr-x---  1 ftp_mont System       918234 Jan 20 15:44 Mende_Mende_COMJUN.m4a

file niewer to older

Arte you sure? I think the output should contain only the filenames.

If you want to inverse the order just drop the -r switch from the sort command.

I list files on server ftp and, it gives me a list of file in alphabetic order.
I want this list of file in order like "ls -t", the niewer in first and the older last.

INPUT:

drwxr-x---  2 ftp_mont System            0 Sep 30 09:16 .
drwxr-x---  2 ftp_mont System            0 Jul 15  2009 ..
-rwxr-x---  1 ftp_mont System    250324761 Sep 27 15:47 Dossier_secheresse-H264-1.mov
-rwxr-x---  1 ftp_mont System    343998791 Sep 30 09:20 manif laitiers-H264.mov
-rwxr-x---  1 ftp_mont System    169097455 Sep 27 15:55 Mende_Mende-H264.mov
-rwxr-x---  1 ftp_mont System     12063097 Sep 27 15:32 Mende_Mende_ITW.mov

OUTPUT:

-rwxr-x---  1 ftp_mont System    343998791 Sep 30 09:20 manif laitiers-H264.mov
-rwxr-x---  1 ftp_mont System    169097455 Sep 27 15:55 Mende_Mende-H264.mov
-rwxr-x---  1 ftp_mont System    250324761 Sep 27 15:47 Dossier_secheresse-H264-1.mov
-rwxr-x---  1 ftp_mont System     12063097 Sep 27 15:32 Mende_Mende_ITW.mov

I want to do this because i get file by mirroring with wget and wget have a problem with the files with accent...
I thank to rename the file downloaded by wget with the list of file on server then i have 1 file text where have the file in order "by time"

let i=0;for file in /Users/macbook/oberon/*;do echo "$file";let i=i+1;var="$(cat $temp/oberon_trie.txt | sed 's/ *$//g' | sed -n "$i"p)";mv "$file" "/Users/macbook/oberon/$var";done

Just a thought: Can't you use use sftp to access the machine? Usually sftp supports ls -t or ls -lt or even ls -ltr.

i haven't sftp on mac os x

Are you sure? What happens if you type "man sftp" in the terminal?
SFTP in Mac OS X
Mac OS X Manual Page For sftp(1)

I'm sure.....
I install the dmg....now i have the utility SFTP