To search lines between two timestamps in a file

I have a log file where every line starts with a time stamp. I have to extract lines from the file within a given time stamp.
For example:
IF the file is like this:

2012-08-19 10:34:03,446|WebContainer : 56|OrderHeaderDaoImpl|findByKeys|26|
2012-08-20 11:34:03,463|WebContainer : 56|OrderHeaderDaoImpl|addData|17|
2012-08-21 12:34:03,694|WebContainer : 56|LineItemRules|lineItemRules|231|
2012-08-22 13:34:03,704|WebContainer : 56|ShipmentServiceImpl|updateShipmentsAndNotes|10|
2012-08-22 16:34:03,842|WebContainer : 56|XFILI212ClientImpl|calculateTax|138|
2012-08-23 10:34:03,842|WebContainer :    

and I need the data between the time stamps 2012-08-20 11:30 and 2012-08-22 16:00
then I should be able to extract the data lines:

2012-08-20 11:34:03,463|WebContainer : 56|OrderHeaderDaoImpl|addData|17|
2012-08-21 12:34:03,694|WebContainer : 56|LineItemRules|lineItemRules|231|
2012-08-22 13:34:03,704|WebContainer : 56|ShipmentServiceImpl|updateShipmentsAndNotes|10|

I have tried using awk -v but if the time stamp is not found in the file it is giving some false output. Please help.

Thanks in Advance.

If you know the exact timestamp, you can use this:

sed -n '/2012-08-20 11:34/,/2012-08-22 16:34/p' file

with awk :

b="2012-08-20 11:34:03"
e="2012-08-22 13:34:03"
awk -v "b=$b" -v "e=$e" -F ',' '$1 == b,$1 == e' file

(edit) correction :

b="2012-08-20 11:34:03"
e="2012-08-22 13:34:03"
awk -v "b=$b" -v "e=$e" -F ',' '$1 >= b && $1 <= e' file

With gawk:

gawk -F, -v start='2012-08-20 11:30:00' -v end='2012-08-22 16:00:00' '
BEGIN{
OFS=FS
gsub(/[-:]/," ",start);gsub(/[-:]/," ",end)
start=mktime(start);end=mktime(end)
}
{
 t=$1
 gsub(/[-:]/," ",t)
 linetime=mktime(t)
 if(linetime>=start && linetime<=end)
  print
}' infile

This works as well:

cat file | awk -F, '{ if ($1>"2012-08-20 11:30" && $1<"2012-08-22 16:00") print }'

It doesn't work when I store the values in variables though.

Try with variable..

 awk -F, -v STD="2012-08-20 11:30" -v ED="2012-08-22 16:00" '{ if ($1 > STD && $1 < ED) print }' file

That doesn't work for me:

awk -F, -v STD="2012-08-20 11:30" -v ED="2012-08-22 16:00" '{ if ($1 > STD && $1 < ED) print }' testfile
awk: syntax error near line 1
awk: bailing out near line 1

Don't know what's issue....:confused:

Here it works perfect...

$ awk -F, -v STD="2012-08-20 11:30" -v ED="2012-08-22 16:00" '{ if ($1 > STD && $1 < ED) print }' file
2012-08-20 11:34:03,463|WebContainer : 56|OrderHeaderDaoImpl|addData|17|
2012-08-21 12:34:03,694|WebContainer : 56|LineItemRules|lineItemRules|231|
2012-08-22 13:34:03,704|WebContainer : 56|ShipmentServiceImpl|updateShipmentsAndNotes|10|

$ cat file
2012-08-19 10:34:03,446|WebContainer : 56|OrderHeaderDaoImpl|findByKeys|26|
2012-08-20 11:34:03,463|WebContainer : 56|OrderHeaderDaoImpl|addData|17|
2012-08-21 12:34:03,694|WebContainer : 56|LineItemRules|lineItemRules|231|
2012-08-22 13:34:03,704|WebContainer : 56|ShipmentServiceImpl|updateShipmentsAndNotes|10|
2012-08-22 16:34:03,842|WebContainer : 56|XFILI212ClientImpl|calculateTax|138|
2012-08-23 10:34:03,842|WebContainer :

Gnu sed

sed -r '/2012-08-2[0-2] (11:[3-5][0-9]|1[2-5]:[0-5][0-9]|16:00)/!d' infile