Get the oldest date based on date in the filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get the oldest date based on date in the filename
# 1  
Old 06-02-2011
Get the oldest date based on date in the filename

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:
Code:
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:
Code:
$ 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
# 2  
Old 06-02-2011
How about this:
Code:
$
$ ls -1
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
k1
$
$
$
$ cat k1
#! /bin/ksh
set -A files -s - *+([0-9])*
filename=${files[0]}
piece=${filename%_*}
echo filename = $filename
echo piece = $piece
exit 0
$
$
$ ./k1
filename = 20110501_1.dat
piece = 20110501
$

This User Gave Thanks to Perderabo For This Post:
# 3  
Old 06-02-2011
Your requirements can produce a very involved solution.

Since you already know how to sort/head/cut, it seems that your major issue is with the regular expression.

This piece of code will produce a regular expression to display strings with dates in the format YYYYMMDD:
Code:
#!/usr/bin/ksh
mYYYY_19xx='19[5-9][0-9]'
mYYYY_20xx='20[01][0-9]'
mYYYY="${mYYYY_19xx}|${mYYYY_20xx}"
m0x='0[1-9]'
m1x='1[0-9]'
m2x='2[0-9]'
m3x='3[01]'
mMM_1x='1[0-2]'
mMM="${m0x}|${mMM_1x}"
mDD="${m0x}|${m1x}|${m2x}|${m3x}"
egrep "(${mYYYY})(${mMM})(${mDD})" input_file

From here you can change this code to include other data manipulations, including other date formats (MMDDYY, DDMMYYYY, etc.).
This User Gave Thanks to Shell_Life For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to change existing date to current date in a filename?

Suppose i have a list of files in a directory as mentioned below 1. Shankar_04152019_ny.txt 2. Gopi_shan_03122019_mi.txt 3. Siva_mourya_02242019_nd.txt .. . . . . 1000 . Jiva_surya_02282019_nd.txt query : At one shot i want to modify the above all filenames present in one path with... (4 Replies)
Discussion started by: Shankar455
4 Replies

2. Shell Programming and Scripting

Delete oldest folder based on folder named as date

Hi, I have a script doing backup to synology server, the script create new folder each day with the date as being folder name i.e. 2018-07-30. Just before creating the new folder I want the script to find the oldest folder from the list and delete it including its content. for example... (3 Replies)
Discussion started by: humble_learner
3 Replies

3. HP-UX

HP/UX command to pull file name/date based on date

HI, Can anyone tell me how to pull the date and file name separated by a space using the find command or any other command. I want to look through several directories and based on a date timeframe (find -mtime -7), output the file name (without the path) and the date(in format mmddyyyy) to a... (2 Replies)
Discussion started by: lnemitz
2 Replies

4. UNIX for Dummies Questions & Answers

Find the count of files by last created date based on the given date range

My unix version is IBM AIX Version 6.1 I tried google my requirement and found the below answer, find . -newermt “2012-06-15 08:13" ! -newermt “2012-06-15 18:20" But newer command is not working in AIX version 6.1 unix I have given my requirement below: Input: atr files: ... (1 Reply)
Discussion started by: yuvaa27
1 Replies

5. Shell Programming and Scripting

Script to determine Date,TotalFile,total size of file based on date

I have file listed like below -rw-r--r--+ 1 test test 17M Nov 26 14:43 test1.gz -rw-r--r--+ 1 test test 0 Nov 26 14:44 test2.gz -rw-r--r--+ 1 test test 0 Nov 27 10:41 test3.gz -rw-r--r--+ 1 test test 244K Nov 27 10:41 test4.gz -rw-r--r--+ 1 test test 17M Nov 27 10:41 test5.gz I... (5 Replies)
Discussion started by: krish2014
5 Replies

6. UNIX for Dummies Questions & Answers

Copy log based on from-date and to-date

Hi all, i go a customer support requirement where i need to scan several files based on from/to date like 1-oct to 2-oct please help... (3 Replies)
Discussion started by: AbhiJ
3 Replies

7. Shell Programming and Scripting

How to append date to filename, but base it on yesterday's date?

Hello, I'd like to write a monthly archive script that archives some logs. But I'd like to do it based on yesterday's date. In other words, I'd like to schedule the script to run on the 1st day of each month, but have the archive filename include the previous month instead. Here's what I... (5 Replies)
Discussion started by: nbsparks
5 Replies

8. Shell Programming and Scripting

Find the latest file based on the date in the filename

Hi, We've a list of files that gets created on a weekly basis and it has got a date and time embedded to it. Below are the examples. I want to find out how to get the latest files get the date and time stamp out of it. Files are PQR123.PLL.M989898.201308012254.gpg... (1 Reply)
Discussion started by: rudoraj
1 Replies

9. Shell Programming and Scripting

Move files based on date in filename

I know this gets covered quite a bit in the forum and I think there is enough there for me to figure out how to do what I am trying to do, I just don't think I would do it very efficiently so I am going to ask the question... I have database log files with date and time stamps in the file like ... (7 Replies)
Discussion started by: slatoms
7 Replies

10. Shell Programming and Scripting

sort files by date, delete oldest, if total size bigger than

hello people i need your help please i want to achieve the following with the simplest, most efficient shell-tools: i have a directory with a lot of files from users. the script should check which partition the dir is on if the partition with the directory is more than 90% full ... (2 Replies)
Discussion started by: scarfake
2 Replies
Login or Register to Ask a Question