Computing.Net > Forums > Unix > Sort by Date-timestamps in filename

Sort by Date-timestamps in filename

Reply to Message Icon

Original Message
Name: Chindhu
Date: July 3, 2006 at 02:57:14 Pacific
Subject: Sort by Date-timestamps in filename
OS: Windows XP
CPU/Ram: 1.6Ghz, 1GB RAM
Model/Manufacturer: DELL
Comment:

Hi,

I am new to Unix shell scripting, starting to learn it. Can you please help me with this immediate requirement to code.. The requirement is as given below.

In a directory say Y, I have files like this.

PP_100000_28062006_122731_746.dat
PP_100000_28062006_122731_745.dat
PP_100000_28062006_122734_745.dat
PP_100000_28062006_122732_745.dat
PP_100000_28062006_122801_745.dat

QQ_100001_28062006_122733_745.dat
QQ_100001_28062006_122731_745.dat


RR_100002_28062006_122731_745.dat
RR_100002_28062006_122731_745.dat
RR_100002_28062006_122731_745.dat

format: <type>_<somesequence>_DDMMYYYY_HHMMSS_<organisationID>

Now, I will have an input parameter coming in as 'Directory path' (path to Y) and type (can be PP, QQ or RR. If type is NULL, means I need to process all the 3 types)

Assuming we get type as PP

Now I need to pick up files of this type. We can see there are 5 files matching this string.
Now, I need to check the date and time stamp as available in the file names (and not the unix system date timestamp), compare it, and pick up the file with the lowest date timestamp in its name, first - and process it.
As you see I have 2 files with same timestamp here - PP_100000_28062006_122731_746.dat and PP_100000_28062006_122731_745.dat. In this case I have to process it in ascending order of <organisationID>. ie, PP_100000_28062006_122731_745.dat first, followed by PP_100000_28062006_122731_746.dat

Once processed, I have to pick the next lowest date time stamp file for processing.

I will be archiving successfully processed files in another directory, and renaming failed files to say FAILED (suffix) so that it wont be picked up again.

Any sample code/ pointers/ suggestions would be of great help.

Thanks a lot in advance,
Chindhu


Report Offensive Message For Removal


Response Number 1
Name: nails
Date: July 4, 2006 at 11:17:16 Pacific
Reply: (edit)

I'll help you get started. Assume all the files starting with PP exist in the Y directory. Parse the filename and set field 3 to the YYYYMMDD format. Build a new file name and rename each file. Finally, sort the new filenames - delimited by _ - by the data and time fields (3 & 4) and the organization ID field (5):

#!/bin/ksh

cd Y
ls -1 PP*|while read line
do
set - $(IFS="_"; echo $line)
# new file starts with lowercase "x"
f1=x$1
f2=$2

yr=$(echo $3|cut -c5-8)
mm=$(echo $3|cut -c3-4)
dd=$(echo $3|cut -c1-2)
f3=$yr$mm$dd

f4=$4
f5=$5
nf="$f1"_"$f2"_"$f3"_"$f4"_"$f5"
mv $line $nf
done
ls xPP*|sort -t "_" -k 3,4 -k 5,5


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Sort by Date-timestamps in filename

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge