Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:


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:


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


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.