I have a list of files that take on the format ABCDE_yymmdd and wish to sort them in ascending date order. I can't use the unix time stamp for the file as this could possibly be different from the date given in the file name.
Does anyone know of any way this can be done using unix shell scripting and/or awk(nawk)?
What i already have:
constructFileList()
{
if [ -f $DataDir/$FileListFile ]
then
rm -f $DataDir/$FileListFile
fi
for FILE in `ls -1 $DataDir/ABCDE_??????.dat`
do
echo $(basename $FILE)|nawk -f sort.awk
done |sort|while read FILENAME
do
echo ${FILENAME#??????} >> $DataDir/$FileListFile
done
}
Wow, impressive, so much easier than what i was doing thanks anbu23, can you just give a brief explanation of what the -t "_" -k2 does, i would like to understand what I am using so I can build my knowledge.
Edit: I think i get what the -t option is for, to spilt the string to be able to sort on just the date part?
What if the only yy would be above 2000, so 07, 08, 09 etc, would it make the solution simplier? If being honest its unlikely we will have a file pre 2000.
I have read the above but am not clear on one thing, I get the .5 .6 which basically equates to the last two fields of the string, i.e. the year, and sorts on them first and the same for .3 .4 and .1 .2, but I don't really understand the 2. and 3. part. While I know this refers to the field and the part after the . refers to the character within the field, I don't understand how it can be 2.5 and 2.6, how are fields split up in the string, i.e. what exactly is field 2 and field 3.
Sorry I did read the above and have had a look for some further text and examples and while a bit clearer I still don't fully understand it
With '_' as field separator, the date is field 2 is in the form ddmmyy.ok
-k2.5,2.6 : Chars 5 to 6 of field 2 -> yy
-k2.3,2.4 : Chars 3 to 4 of field 2 -> mm
-k2.1,2.2 : Chars 1 to 2 of field 2 -> dd