grep between date ranges in a log grep between date ranges in a log shell shell

grep between date ranges in a log


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


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


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