![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Perl: Extracting date from file name and comparing with current date | MKNENI | Shell Programming and Scripting | 4 | 03-26-2008 12:01 PM |
| Extracting Filename from Fullpath | njoshi | UNIX for Dummies Questions & Answers | 3 | 04-19-2007 02:34 PM |
| extracting files using date in ksh | pavan_test | UNIX for Dummies Questions & Answers | 1 | 04-04-2006 12:29 PM |
| extracting only the filename without its extenstion | lotus123 | UNIX for Dummies Questions & Answers | 3 | 08-25-2005 07:44 AM |
| filename to contain date | rkap | Shell Programming and Scripting | 4 | 04-06-2005 05:53 AM |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
|||
|
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
Edit: fergeddit, radoulov's solution is much better, as usual /-: Last edited by era; 04-23-2008 at 05:55 AM. Reason: got beaten up by R again |
|
|||
|
Quote:
Code:
# print - "yesterdays date: $(TZ=GMT+24; date)" # print - "one week ago: $(TZ=GMT+$(( 24*7 )); date)" |
|
|||
|
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: 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.... |
|||
| Google UNIX.COM |