The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-23-2008
laiko laiko is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 8
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 (permalink)  
Old 04-23-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,854
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 08:05 AM..
  #3 (permalink)  
Old 04-23-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 08:55 AM.. Reason: got beaten up by R again
  #4 (permalink)  
Old 04-23-2008
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
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
  #5 (permalink)  
Old 04-23-2008
denn denn is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 96
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 (permalink)  
Old 05-09-2008
laiko laiko is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 8
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 (permalink)  
Old 11-25-2008
prsshini prsshini is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 20
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:04 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0