I am using ksh93 on Solaris.
Ok, this may seem like a simple request at first. I have a directory that contains sets of files with a YYYYMMDD component to the name, along with other files of different filespecs. something like this:
20110501_1.dat
20110501_2.dat
20110501_3.dat
20110502_1.dat
20110502_2.dat
20110502_3.dat
20110503_1.dat
20110503_2.dat
20110503_3.dat
I need to get the date component of the filename of the oldest set, and try to be flexible for future filename patterns. So far I have this:
$ ls |grep '.*[0-9]\{8\}.*'|sort |head -1|cut -c1-8
$ 20110501
problems with this:
- grep is looking for a string of 8 numbers anywhere in the name (not really a date format-what if future files also have numbers in the name).
- the cut assumes the date string of characters is in the beginning of the filename but I need to allow for it anywhere in the filename.
- Whenever I start building long pipelines that is usually a red flag that I should look for a more efficient way.
Ideally I would like to base the search on a filename mask, i.e. I read from another file or a database table that the filespec I am looking for is "filename1_YYMMDDHHMM.dat", or "YYYYMMDD_filename1.dat" where I would then run the command using that filespec to get the oldest set in order to process the sets in date order.
Perhaps the right find command or some awk magic?
I would appreciate any ideas on making this flexible.
Thanks for any info.
Gary