![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | 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 !! |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| 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 04:01 PM |
| Extracting Filename from Fullpath | njoshi | UNIX for Dummies Questions & Answers | 3 | 04-19-2007 05:34 PM |
| extracting files using date in ksh | pavan_test | UNIX for Dummies Questions & Answers | 1 | 04-04-2006 03:29 PM |
| extracting only the filename without its extenstion | lotus123 | UNIX for Dummies Questions & Answers | 3 | 08-25-2005 10:44 AM |
| filename to contain date | rkap | Shell Programming and Scripting | 4 | 04-06-2005 08:53 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | 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. |
|
||||
|
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 08: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.... |
|
||||
|
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. |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|