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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sort file text by date as a "ls -t"
# 1  
Old 10-03-2009
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
Code:
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...

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[i]};
for(i=1;i<=length(b);i++){print b[i]};
for(i=1;i<=length(c);i++){print c[i]};
for(i=1;i<=length(d);i++){print d[i]};
for(i=1;i<=length(e);i++){print e[i]};
for(i=1;i<=length(f);i++){print f[i]};
for(i=1;i<=length(g);i++){print g[i]};
for(i=1;i<=length(h);i++){print h[i]};
for(i=1;i<=length(j);i++){print j[i]};
for(i=1;i<=length(k);i++){print k[i]};
for(i=1;i<=length(l);i++){print l[i]};
for(i=1;i<=length(m);i++){print m[i]}
}' | awk 'NF{print $9,$10,$11,$12,$13,$14,$15}' | awk '{a[NR]=$0}END{for(i=FNR;i>=0;i--){print a[i]}}'

Thanx.
# 2  
Old 10-03-2009
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.
# 3  
Old 10-03-2009
Code:
awk '!/^d/ {
  for (i=6; i<=NF; i++)
    printf "%s", $i (i == NF ? RS : FS)
  }' infile | 
      sort -Mr |
        cut -d\  -f4-

# 4  
Old 10-03-2009
Code:
#!/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).

Last edited by Scrutinizer; 10-03-2009 at 07:00 PM..
# 5  
Old 10-04-2009
thank you for your answer scrutinizer but i'm on mac os X and the options of date don't work...
# 6  
Old 10-04-2009
Bummer. Then perhaps you could use a shell function to convert to epoch time for sorting. E.g. like so:
Code:
#!/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 Smilie)

Last edited by Scrutinizer; 10-04-2009 at 07:43 AM..
# 7  
Old 10-04-2009
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Beginners Questions & Answers

Extract delta records using with "comm" and "sort" commands combination

Hi All, I have 2 pipe delimited files viz., file_old and file_new. I'm trying to compare these 2 files, and extract all the different rows between them into a new_file. comm -3 < sort file_old < sort file_new > new_file I am getting the below error: -ksh: sort: cannot open But if I do... (7 Replies)
Discussion started by: njny
7 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

Using sed to find text between a "string " and character ","

Hello everyone Sorry I have to add another sed question. I am searching a log file and need only the first 2 occurances of text which comes after (note the space) "string " and before a ",". I have tried sed -n 's/.*string \(*\),.*/\1/p' filewith some, but limited success. This gives out all... (10 Replies)
Discussion started by: haggismn
10 Replies

5. Shell Programming and Scripting

sort text having delimiter with "|" (pipe)

i am having text file below NARGU S S 12358 SALES REP |22| Acccount/s RAJU R B 64253 SALES REP |12| Acccount/s RUKMAN S 32588 SALES REP |10| Acccount/s NARGUND S S 12356... (3 Replies)
Discussion started by: suryanarayana
3 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

Meaning of "b" modifier in "sort" command

I need to sort the following file by the rhdiskpower devices in the last column: Total_MB Free_MB OS_MB Name Failgroup Library Label UDID Product Redund Path 1024 851 1024 OCRVOT1_0000 OCRVOT1_0000 System UNKNOWN ... (3 Replies)
Discussion started by: wjssj
3 Replies

8. AIX

xx=`date +"%a %b %d"`;rsh xxx grep "^$XX" zzz ?

AIX 4.2 I am trying to do an rsh grep to search for date records inside server logs by doing this : xx=`date +"%a %b %d"` rsh xxx grep "^$XX" zzz gives : grep: 0652-033 Cannot open Jun. grep: 0652-033 Cannot open 11. But if I do : xx=`date +"%a %b %d"` grep "^$XX" zzz it works... (2 Replies)
Discussion started by: Browser_ice
2 Replies

9. Solaris

"mail" command sort by date

Hello experts, I am using SunFire T200. When I start reading the mail with "mail" command it comes older mail first. From MAILER-DAEMON Sat Mar 28 06:02:48 2009 Return-Path: <MAILER-DAEMON@emarn1> Received: from localhost (localhost) .... .... I want to see the most recent mail... (1 Reply)
Discussion started by: thepurple
1 Replies

10. UNIX for Advanced & Expert Users

add seconds to: date"|"time"|"HHMMSS

Hey all, I have a shell that invokes a AWK. In this AWK i want invoke a function that receives 3 parameters: date: 20080831 time: 235901 duration: 00023 that function receive this 3 parameters and sum to this value two more seconds: 2008083123590100025 Remember that in case that... (3 Replies)
Discussion started by: anaconga
3 Replies
Login or Register to Ask a Question