Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have done a few simple things with awk but I cannot find a way to do this! I have a text file containing entries named with year, julian date and time as such:

blah_2012251_130500_blah  
blah_2012251_131000_blah  
blah_2012251_131500_blah  
...  
blah_2012253_063000_blah  

and I am trying to find a way to create a new file with entries within a specific time range only. For example, year 2012 and day 251 at 2:02pm (140200) to 2012 day 252 to at 6:07am (060700) should contain files to span the times:

blah_2012251_140000_blah  
blah_2012251_140500_blah
...  
blah_2012252_060500_blah
blah_2012252_061000_blah

Also, the files do not always increment every 5 minutes like in this example, so it needs to be an open range.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

the following should work..adjust field ordinals 2 and 3 as needed

 awk -F'_' 'int($2""$3) > 2012252140600 && int($2""$3) < 2012252180700' filename
share|improve this answer
    
Thank you for the quick response! This way is very simple and works great! I'm sorry I wasn't clear before but I also need to include the 1 file BEFORE the starting time in the case that the starting time does not match the file name exactly. Meaning, for time 140600, if there is not a file called 140600 I need the file before it, could be 140500 or in some cases even 135900. –  user26332 Oct 25 '12 at 16:18

f1 and f2 are the starting date and time, l1 and l2 are the ending date and time:

awk -F_  -v f1="2012251" -v f2="140200" -v l1="2012252" -v l2="060700" '($2==f1 && $3>=f2) || ($2==l1 && $3<=l2) || ($2>f1 && $2<l1)' file
share|improve this answer
    
Thank you! I probably wasn't clear before but I also need the file before 140200 which is file 140000 (and in some cases could be file 135900, for example). –  user26332 Oct 25 '12 at 16:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.