extracting date from a filename


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers extracting date from a filename
# 1  
Old 04-23-2008
extracting date from a filename

Hi,
I am a beginner in Unix so please bear with me...
I have a directory which has files in format: RECF-YYYY-MM-DD-input. For example, RECF-2008-02-25-input. I need to extract the YYYYY-MM-DD substring from this filename and convert that into date and compare it with a date. How do I do that?
For instance, I have a directory with the following files:
RECF-2008-02-21-input
RECF-2008-02-22-input
RECF-2008-02-23-input
RECF-2008-02-24-input

I need to get the files whose date substrings are greater than 2008-02-22 (Feb 22, 2008) which are RECF-2008-02-23-input and RECF-2008-02-24-input in the examples above. Please help

Thanks in advance.
# 2  
Old 04-23-2008
Code:
$ ls -1
RECF-2008-02-21-input
RECF-2008-02-22-input
RECF-2008-02-23-input
RECF-2008-02-24-input
$ printf "%s\n" *|awk -F- '$2FS$3FS$4>"2008-02-22"'
RECF-2008-02-23-input
RECF-2008-02-24-input

Just re-read your post, you should use date +% ... to print the date in the desired format Smilie

Code:
printf "%s\n" *|awk -F-  '$2FS$3FS$4>d' d="$(date +%Y-%m-%d)"


Or:

Code:
$ printf "%s\n" *|awk '$0>d' d="RECF-$(date +%Y-%m-%d)-input"
$ printf "%s\n" *|awk '$0>d' d="RECF-2008-02-22-input"


Last edited by radoulov; 04-23-2008 at 09:05 AM..
# 3  
Old 04-23-2008
This requires a version of date which can do -d "day" and +%s which -- with my luck -- probably excludes yours. But anyway, here goes. This converts the date in the file name into a canonical date stamp (seconds since Jan 1, 1970, also known as "Unix epoch") which is then easy to compare by whatever you have which can compare two numbers.

Code:
vnix$ ls RECF-*-*-*-input |
cut -d - -f2-4 |
xargs -i date -d '{}' +'%s RECF-{}-input'
1203544800 RECF-2008-02-21-input
1203631200 RECF-2008-02-22-input
1203717600 RECF-2008-02-23-input
1203804000 RECF-2008-02-24-input

The cut command extracts the date; xargs arranges to have those results passed to date one by one, constructing and executing a command like date -d "2008-04-24" +"%s RECF-2008-02-24-input". The %s format specifier is "seconds since epoch"

Edit: fergeddit, radoulov's solution is much better, as usual /-:

Last edited by era; 04-23-2008 at 09:55 AM.. Reason: got beaten up by R again
# 4  
Old 04-23-2008
Quote:
Originally Posted by era
This requires a version of date which can do -d "day" and +%s which -- with my luck -- probably excludes yours.
You can do date arithmetics even if the date command doesn't support it - at least on the day level - quite easily using the TZ variable (i suppose in the following example that you are in the GMT timezone. You will have to introduce a corrective factor if not.):

Code:
# print - "yesterdays date: $(TZ=GMT+24; date)"
# print - "one week ago: $(TZ=GMT+$(( 24*7 )); date)"

bakunin
# 5  
Old 04-23-2008
print - "one week ago: $(TZ=GMT+$(( 24*7 )); date)"
Note: This doesn't work on HP-UX, this method can be used +-24 hrs max for that OS.
Other variants of UNIX that I tried this on previously, this works as expected.
# 6  
Old 05-09-2008
Filtering the filenames in an FTP box

Thanks a lot guys. The AWK works.
Now, I have a new problem. I need to do it in an FTP box. It does not accept 'AWK' commands.
I need to find the files in the FTP box whose filename has dates greater than say 2008-02-22 (Feb 22, 2008). Example:
Code:
ftp -pin <<EOF>>$mylog
   open inbox.mi.com
   quote user $user
   quote pass $pass
   lcd $destination_dir
   cd SOURCE
   -- here, get all the files SOURC-YYYY-MM-DD-bin
   -- whose date (the YYYY-MM-DD substr) is greater than '2008-02-22'
   bye
EOF

So, in my FTP box, I have files like:
SOURC-2008-03-21-bin
SOURC-2008-01-23-bin
SOURC-2008-04-17-bin
SOURC-2008-04-08-bin
SOURC-2008-02-19-bin


I only want to get the files whose date component is greater than 2008-02-22 which, in this example, are:
SOURC-2008-03-21-bin
SOURC-2008-04-17-bin
SOURC-2008-04-08-bin


Thanks in advance....
# 7  
Old 11-25-2008
I have a similar issue.
I grep for a pattern in a list of files.
"grep -i -l $pattern *.datx*"
it may give me n number of files.

say for eg, it gives 2 files.
lock_eicu_20071228_00000000.dat_20071228_05343100
lock_eicu_20080501_00000000.dat_20080501_05343900

out of these 2 files I need to get the latest file according to the date present after the .dat extn.
so in the above eg, i need to get the filename
lock_eicu_20080501_00000000.dat_20080501_05343900

can u please help.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extracting filename

I am using bash and have a filename with a path and extension and want to extract just the filename Have used the following code, oflna gives the file name with extension, but now neet to remove the .texi at the end. oflna=${flnm##*/} oflnb=${${flnm##*/}%.*} echo "flnm: $flnm" echo... (1 Reply)
Discussion started by: Danette
1 Replies

2. 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

3. 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

4. Shell Programming and Scripting

Extracting a portion of the filename

Hi I would like to extract the first portion of filename from a list of files. The filename pattern is of the form 123456789_TEXT_TEXT_TEXT_.csv. I want to extract just the numerical portion of this filename from the list of files and then output this into another text file. K (6 Replies)
Discussion started by: kamal_p_99
6 Replies

5. Shell Programming and Scripting

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: 20110501_1.dat 20110501_2.dat 20110501_3.dat... (2 Replies)
Discussion started by: gary_w
2 Replies

6. Shell Programming and Scripting

extracting multiple variables from a filename.

hi all, I'm trying to automate some tasks and while I've got the script itself working, I'm having difficulties with automatic file detection and associated variable setting... for example, in a directory I've got several files... something along the lines of: xis0_NAME_src.file... (2 Replies)
Discussion started by: u5j84
2 Replies

7. Shell Programming and Scripting

Extracting the Filename

Hi, I need to extract the file name without filetype. Suppose in DIR1 if i have files like F1.txt and F2.DOC then i need to return F1 and F2 only with out file types (txt and DOC). I tried with the following code newname = ` $i | cut -d "'." -f1` but it is giving the error " 0403-006... (1 Reply)
Discussion started by: Raamc
1 Replies

8. Shell Programming and Scripting

Perl: Extracting date from file name and comparing with current date

I need to extract the date part from the file name (20080221 in this ex) and compare it with the current date and delete it, if it is a past date. $file = exp_ABCD4_T-2584780_upto_20080221.dmp.Z really appreciate any help. thanks mkneni (4 Replies)
Discussion started by: MKNENI
4 Replies

9. UNIX for Dummies Questions & Answers

Extracting Filename from Fullpath

Hi, Any help on this would be very appreciated. I capture the full path & filename in a variable like (varFile=/home/user/extfile.txt). Now in my shell script I have to use only the filename part i.e. extfile.txt. How do I extract only the filename part from the variable? Thanks in... (3 Replies)
Discussion started by: njoshi
3 Replies

10. UNIX for Dummies Questions & Answers

extracting only the filename without its extenstion

Hi, I have a requirement that i need to store only the filename without its extension. Can anyone please help me to do this. For Example, i have stored the filename in a varialble called fname. I need to extract all the charecters before the first occurence of the dot. If fname has value... (3 Replies)
Discussion started by: lotus123
3 Replies
Login or Register to Ask a Question