Grep between date ranges in a log

I try to grep all the lines between two date ranges where the dates are formatted as follows: date_time.strftime ("% Y% m% d% H% M"), so to speak, between [201211150821 - 201211150824]

I am trying to write a script that involves searching for strings between these dates:

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

I am trying to find out if something exists on unix where I can find the range in date.

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

Maybe something simple already exists so that I can specify date / timestamp ranges, how can I provide a numerical range for grep?

Thanks!

PS: I can also pass something like 2012111511 (27 | 28 | 29 | [3-5] [0-9]) into the template, but this is specific to the ranges I want, and it is tedious to check different dates every time and it gets harder doing it at runtime.

+7
source share
5 answers

Use awk. Assuming the first token in the string is a timestamp:

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

Perl Solution:

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

(He finds the first ten-digit integer starting with 20 , and prints a line if that integer is within your range of interest. If he doesn't find such an integer, he skips the line. Tweak regex to be more restrictive regarding the current months and hours, etc.)

+2
source

You are looking for a somewhat obscure csplit command (contextual separation):

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

will split all lines between the first and second regular expressions from the file. Most likely, it will be the fastest and shortest if your files are sorted by date (you said that you are grepping logs).

+1
source

Bash Coreutils expression 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 comparative value,

0
source

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

In your example, usage would be:

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

0
source

All Articles