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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Sort by Date-timestamps in filename

Reply to Message Icon

Name: Chindhu
Date: July 3, 2006 at 02:57:14 Pacific
OS: Windows XP
CPU/Ram: 1.6Ghz, 1GB RAM
Product: 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



Sponsored Link
Ads by Google

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

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


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Sort by Date-timestamps in filename

sort by date mm/dd/yyyy www.computing.net/answers/unix/sort-by-date-mmddyyyy/4765.html

Date format to filename www.computing.net/answers/unix/date-format-to-filename/7082.html

Order by Date MM/DD/YY Help Urgent www.computing.net/answers/unix/order-by-date-mmddyy-help-urgent/5576.html