The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Get date and time for past 1 hour from current date spch2o Shell Programming and Scripting 5 08-29-2008 01:32 AM
sort files by date itik Linux 1 06-02-2008 03:25 PM
Processing a log file based on date/time input and the date/time on the log file primp Shell Programming and Scripting 4 03-16-2008 08:23 AM
how to numeric sort on field time rahulspatil_111 Shell Programming and Scripting 1 04-27-2007 08:52 AM
Sort by Date Mudshark Shell Programming and Scripting 2 04-18-2005 09:02 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 11-28-2007
Registered User
 

Join Date: Sep 2006
Location: Sg
Posts: 321
Sort By Date and Time

Hi ,

I would like to list or sort by date and time (the files are named in day and time format) where the latest file will be placed at the bottom and the earliest file be placed at the top. Can anybody help me?

My files are named in the following manner.

EG: abc_071128_144121_data
" abc " is random , " 071128 " depicts the date in yymmdd format, " 144121 " depicts the time in the hhmmss format

Input:
abc_071128_144121_data
xxx_071128_123524_data
fff_071128_122837_data
jjj_071129_010544_data
qqq_071129_002603_data
hhh_071128_120728_data
bbb_071128_113329_data


Ouput:
bbb_071128_113329_data
hhh_071128_120728_data
fff_071128_122837_data
xxx_071128_123524_data
abc_071128_144121_data
qqq_071129_002603_data
jjj_071129_010544_data
Reply With Quote
Forum Sponsor
  #2  
Old 11-28-2007
Smiling Dragon's Avatar
Disorganised User
 
Join Date: Nov 2007
Location: New Zealand
Posts: 734
It's a bit (very) cheap but you could try this:
Code:
#!/bin/sh
for file in *
do
    echo $file | cut -d '_' -f 2,3,4 >> /tmp/sort.$$
done
for file in `sort -n < /tmp/sort.$$`
do
    ls -1 *_${file}
done
rm -f /tmp/sort.$$

Last edited by Smiling Dragon; 11-28-2007 at 08:09 PM. Reason: forgot to clean up
Reply With Quote
  #3  
Old 11-28-2007
Smiling Dragon's Avatar
Disorganised User
 
Join Date: Nov 2007
Location: New Zealand
Posts: 734
Actually, cleaner would be:
Code:
ls -1 * | awk 'BEGIN { FS="_" } { print $2,$3,$4,$1}' | sort -n | awk 'BEGIN { OFS="_" } { print $4,$1,$2,$3}'

Last edited by Smiling Dragon; 11-28-2007 at 08:25 PM.
Reply With Quote
  #4  
Old 11-28-2007
Moderator
 

Join Date: Dec 2003
Location: /dev/fl
Posts: 1,061
The following works for the dataset provided:

Code:
sort -t'_' -k2 -k3  datasetfile
Have not tested it on boundary cases.
Reply With Quote
  #5  
Old 11-28-2007
Registered User
 

Join Date: Feb 2006
Location: Schenectady, NY
Posts: 130
Just to throw this out there...if the files are created in a specific time order irrespective of name, then the ls -t command would work nicely. With Solaris, the last modification time is used, but the -u option would use the file's access time.

Check your local man pages.
Reply With Quote
  #6  
Old 11-28-2007
Registered User
 

Join Date: Jun 2007
Location: Beijing China
Posts: 495
sort

Hi,

Try sort as follow:

Code:
sort -t "_" +1 filename
Reply With Quote
  #7  
Old 11-29-2007
Registered User
 

Join Date: Sep 2006
Location: Sg
Posts: 321
Thanks all for the codes.
It's working now !!
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:22 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0