Passing variables to awk

Hi guys,

I need to fetch data from logfile between two given dates,i got the below code from our forum.It works perfect,but i need to enter the value dynamically to awk while running.

awk '/2012 Jun/{p=1}!/2012 Jul/ && prev~/2012 Jul/ && p{p=0}{prev=$0}p' file

i tried the below code,but it not accepting the variable which i pass,displays all the lines in the file.

read start
read end
awk -v d="$start" -v d1="$end" '/d/{p=1}!/d1/ && prev~/d1/ && p{p=0}{prev=$0}p' file
ex:start=2013-06-26
    end=2013-08-01

Please advice me,thank u guys

Try this:

awk -v d="$start" -v d1="$end" '$0 ~ d{p=1} $0 !~ d1 && prev ~ d1 && p{p=0}{prev=$0}p' file
1 Like

Hi Thanks for ur quick response:) :b:.It worked.could u pls say me how the code works.

$0 ~ d is a match for regular expressions, if the record $0 contains the value of the variable d the condition is true.

A dirty trick:

awk '/'${start}'/{p=1}!/'${end}'/ && prev~/'${end}'/ && p{p=0}{prev=$0}p' file