Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.datQQ_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.datformat: <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.datOnce 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

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=$2yr=$(echo $3|cut -c5-8)
mm=$(echo $3|cut -c3-4)
dd=$(echo $3|cut -c1-2)
f3=$yr$mm$ddf4=$4
f5=$5
nf="$f1"_"$f2"_"$f3"_"$f4"_"$f5"
mv $line $nf
done
ls xPP*|sort -t "_" -k 3,4 -k 5,5

![]() |
![]() |
![]() |

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