![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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 |
|
#3
|
||||
|
||||
|
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. |
|
#4
|
|||
|
|||
|
The following works for the dataset provided:
Code:
sort -t'_' -k2 -k3 datasetfile |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
|||
|
|||
|
sort
Hi,
Try sort as follow: Code:
sort -t "_" +1 filename |
|
#7
|
|||
|
|||
|
Thanks all for the codes.
It's working now !! |
|||
| Google The UNIX and Linux Forums |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|