Using variable inside awk

I am trying to print the lines with pattern and my pattern is set to a variable express

awk '/$express/{where=NR;print}' test2.log

I am not getting any data even though i have the data with the pattern. Can seomeone correct me with the awk command above?

Use " instead of ' or awk -v. There is a search function in this forum - this is asked quite often, also for sed.

I am using awk -v in a bash file. unable to get the result. Will it be teh same in a bash file?

Did you try using " as zaxxon said ???
ex:

awk "/$express/{where=NR;print}" test2.log

My total code is like this. I am using this bash file to get the data that is 5 hrs before the current time.


#!/bin/bash
genexpression()
{
stime=`date '+ %H'`
stime=`expr $stime \- 5`
if (( $stime < 10 ))
then
stime=0${stime}
fi
export express=`date '+%m/%d/%Y'`\ $stime
}
genexpression
echo $express
awk "/$express/{print}" test.log

the statement is bailing out here. where as it is working fine if i pass a string to the variable like express='Parameters'

the output is coming like this:

08/06/2009 07
awk: syntax error near line 1
awk: bailing out near line 1

Before putting into awk , check whether the variable contain any thing (i mean non space ) , some thing like this :

if [ "$variable" = " " ]; then
#  variable is space 
else
awk ...........
fi

i am getting the error as follows. I am able to echo the result of the variable express correctly.

08/06/2009 07
test.sh: line 22: syntax error near unexpected token `else'
test.sh: line 22: `else'

please post your script so we can better help you

Can You Try something like this :

if [ "$express" = " " ]; then
echo "space"
else
awk -v ex="$express"  '/ex/ {print ex}'  rem.txt
fi

If wont work , Please post your complete script and the output your expecting.

My basic requirement is to grep for the previous hour data. and my timestamp format is something like thisshown below.

(08/06/2009 05:48:45.992)(:)  
(08/06/2009 05:48:46.641)(:) 

as there is a zero before the Hr, i can't just use

`date '+ %m/%d/%Y H'`

Instead i am using a bash file as shown below. I am able to get the date format but while using awk i am getting error. Please help me if there is any alternative for this requirement or any change needed in awk format? I am using sunOS box.

#!/bin/bash
genexpression()
{
stime=`date '+ %H'`
stime=`expr $stime \- 1`
if (( $stime < 10 ))
then
stime=0${stime}
fi
export express=`date '+%m/%d/%Y'`\ $stime
}
genexpression
echo $express
awk "/$express/{print}" test.log

im not sure what you meant by "you can't use". Maybe you missed the % sign

date +"%m/%d/%Y %H"

I did use the % but the grep is failing because it identifies hrs as teh file name and error out as ' unable to open 06' if 06 is the hrs.

---------- Post updated at 09:26 AM ---------- Previous update was at 09:23 AM ----------

adding to that i need the previous hr data. suppose if the current time is 08/06/2009 09:00
I need the data from 08/06/2009 08:00 to 08/06/2009 08:59

some thing like this :

#!/bin/bash
genexpression()
{
stime=`date '+ %H'`
stime=`expr $stime \- 5`
if (( $stime < 10 ))
then
stime=0${stime}
fi
export express=`date '+%m/%d/%Y'`\ $stime
}
genexpression

echo $express

t=`grep -w "$express" log.txt | wc -l`

if [ $t -ne 0 ];then
echo "suces"
else
echo "fail"
fi

See my Four Ways to Pass Shell Variables in AWK

you can tell shell to make date as a whole string and not separate it.

grep "`date +"%m/%d/%Y %H" -d "1 hour ago"`" file

Panyam, Its working perfectly. Thanks a lot!

proper computation of hour is needed so it can handle 00:00:00 (12AM) minus one hour.
wouldnt be posible with 00-1 unless you have different log for the day ofcourse

Ryandegreat,
It is giving error message as follows.

ksh: : cannot execute
grep: can't open %H -d 1
grep: can't open hour
grep: can't open ago

Panyam,
Will the code work for all the timings? what about 1 am?

it works on me can you check your script again..

-bash-3.2$ cat file
(08/06/2009 05:48:45.992)(:)
(08/06/2009 05:48:46.641)(:)
(08/06/2009 15:00:00
(08/06/2009 15:59:00
-bash-3.2$  grep "`date +"%m/%d/%Y %H" -d "1 hour ago"`" file
(08/06/2009 15:00:00
(08/06/2009 15:59:00
-bash-3.2$ date +"%m/%d/%Y %H"
08/06/2009 16
-bash-3.2$

---------- Post updated at 10:04 PM ---------- Previous update was at 10:03 PM ----------

dont forget the qoute and backticks

i tried the same example that you gave. Got error again. I don't think -d option supported on my SunOS box.