7

I am trying to grep all the lines between 2 date ranges , where the dates are formatted like this : date_time.strftime("%Y%m%d%H%M") so say between [201211150821 - 201211150824]

I am trying to write a script which involves looking for lines between these dates:

cat <somepattern>*.log | **grep [201211150821 -  201211150824]** 

I am trying to find out if something exists in unix where I can look for a range in date.

I can convert dates in logs to (since epoch) and then use regular grep with [time1 - time2] , but that means reading each line , extracting the time value and then converting it etc .

May be something simple already exist , so that I can specify date/timestamp ranges the way I can provide a numeric range to grep ?

Thanks!

P.S: Also I can pass in the pattern something like 2012111511(27|28|29|[3-5][0-9]) , but thats specific to ranges I want and its tedious to try out for different dates each time and gets trickier doing it at runtime.

2
  • Simplest what you can do is gawk + parsing the date into epoch time + filtering in gawk as well Nov 21, 2012 at 19:45
  • 1
    Regular expressions have no concept of numeric ranges. There are just characters. If you don't want to create a specific regex every time, grep is not the right tool.
    – Tomalak
    Nov 21, 2012 at 19:58

5 Answers 5

2

Use awk. Assuming the first token in the line is the timestamp:

awk '
BEGIN { first=ARGV[1]; last=ARGV[2]; }
$1 > first && $1 < last { print; }
' 201211150821 201211150824
2

A Perl solution:

perl -wne 'print if m/(?<!\d)(20\d{8})(?!\d)/
                      && $1 >= 201211150821 && $1 <= 201211150824'

(It finds the first ten-digit integer that starts with 20, and prints the line if that integer is within your range of interest. If it doesn't find any such integer, it skips the line. You can tweak the regex to be more restrictive about valid months and hours and so on.)

1

You are looking for the somewhat obscure 'csplit' (context split) command:

csplit '%201211150821%' '/201211150824/' file

will split out all the lines between the first and second regexps from file. It is likely to be the fastest and shortest if your files are sorted on the dates (you said you were grepping logs).

0

Bash + coreutils' expr only:

export cmp=201211150823 ; cat file.txt|while read line; do range=$(expr match "$line" '.*\[\(.*\)\].*'); [ "x$range" = "x" ] && continue; start=${range:0:12}; end=${range:15:12}; [ $start -le $cmp -a $end -ge $cmp ] && echo "match: $line"; done

cmp is your comparison value,

0

I wrote a specific tool for similar searches - http://code.google.com/p/bsearch/

In your example, the usage will be:

$ bsearch -p '$[YYYYMMDDhhmm]' -t 201211150821 -t 201211150824 logfile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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