The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


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

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-23-2008
Registered User
 

Join Date: Apr 2008
Posts: 5
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.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-23-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,432
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

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 05:05 AM.
Reply With Quote
  #3 (permalink)  
Old 04-23-2008
era era is online now
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,127
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 05:55 AM. Reason: got beaten up by R again
Reply With Quote
  #4 (permalink)  
Old 04-23-2008
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,037
Quote:
Originally Posted by era View Post
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
Reply With Quote
  #5 (permalink)  
Old 04-23-2008
Registered User
 

Join Date: Jul 2007
Posts: 93
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.
Reply With Quote
  #6 (permalink)  
Old 05-09-2008
Registered User
 

Join Date: Apr 2008
Posts: 5
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....
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:22 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0