Issues with grep -w

I am trying to use grep -w something as shown below.

grep -w "$a" $LOG1

It is not giving me neither any error nor any data. I am facing the issue while i run this command inside a script. But i am getting the data if i run the above command outiside the script.
here $a=08/11/2009 21
i tried outisde something like this and is working.
grep -w "08/11/2009 21" $LOG1

Can someone help me with the grep -w inside the script?

Try this

grep -w $a $LOG1

Giving an error message as it is treating 21 as the file name.

---------- Post updated at 11:27 PM ---------- Previous update was at 11:18 PM ----------

It it is working fine in the script also if i input the data manually like as i shown below.
a='08/11/2009 21'
export a

But it is not working if i am use the script shown below for getting the value into variable $a. Even though i am able to echo $a correctly.

#!/bin/bash
genexpression()
{
stime=`date '+ %H'`
#stime=4;
if (( $stime < 5))
then
stime=`expr $stime \+ 19`
export express=`TZ=CST+24 date '+ %m/%d/%Y'`\ $stime
#stime=`expr $stime \- 5`
else
stime=`expr $stime \- 5`
if (( $stime < 10 ))
then
stime=0${stime}
fi
export express=`date '+%m/%d/%Y'`\ $stime
fi
}
genexpression
echo $express
a=$express
export a
echo $a
grep -w "$a" LOG.txt

While using only the last part from your script, works fine for me.

a='08/11/2009 21'
echo $a
grep -w "$a" t1

Kindly make the above as the new script, and check what is the behavior, and report it here.

And why are you doing all the above thing ?? [ that is manipulating with the date command, and finally not using it !! ]

Kindly make the issues very clear, by pasting the output also.

I am sorry for the confusion. That line a=08/11/2009 21' should be replaced with a=$express.I will update my post also accordingly.
And i am using the total script to get 5 hrs back data. And the date format in the file is something like this
08/11/2009 09:00:00

---------- Post updated 08-12-09 at 01:37 AM ---------- Previous update was 08-11-09 at 11:52 PM ----------

If it goes to the else condition The above code is working fine. Seems to be an issues with white apces in if part of the code.

Hi rdhanek,

The same script what you have posted working perfectly for me.

Hi Panyam,

here is the code that i am using to grep for 5 hrs back data. As a part of it..if it goes to the else loop it is working fine. but the data is picked by grep if the code executes th efirst pasrt of teh code(i.e, if loop suuceeds)
p.s. In both the cases i am able to echo date format correctly. Not sure this may be a space issue.

#!/bin/bash
genexpression()
{
stime=`date '+ %H'`
if (( $stime < 5 ))
then
stime=`expr $stime \+ 19`
export express=\(`TZ=CST+24 date '+ %m/%d/%Y'`\ $stime
else
stime=`expr $stime \- 5`
if (( $stime < 10 ))
then
stime=0${stime}
fi
export express=`date '+%m/%d/%Y'`\ $stime
fi
}
genexpression
export express
echo $express
t=`grep -w "$express" Service.log | wc -l`

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

rdhanek,

For time being you can use something like this :

awk -v e="$express" '$0 ~ e {print}' LOG.txt

I am not figuring out why the same is not working with "grep".

Hi Panyam,

i don't think the issue is with grep, as i am getting the data if the else part of the code is executed.
The only issue is with first part of the code which i am using to get the yesterday's data, if the current time is less than 5 am.(as i njeed to get 5 hrs back data)
The first part also is printing the $ express correctly. There should an issue with the space somewhere which is causing ultimately grep not to pick any data.

if (( $stime < 5 ))
then
stime=`expr $stime \+ 18`
export express=`TZ=CST+24 date '+%m/%d/%Y'`\ $stime

But the awk statement which i mentioned is working well in both the conditions. You can test that which is similar to grep only.

panyam, i am getting a bailing out error with that awk command.

The same working fine for me ..can you recheck what you have tried.

Hi Panyam,

The awk statement is still giving me bailing out error. i am not sure if it is an issue because my box is SunOS.

Apart from this, the grep -w that you have suggested is working fine for both the instances finally.(it was not working for maual runs. I tried by cronning the script and it just worked fine. Not sure what was the issue when i run it manually. )

Thanks a lot for ur continuous support on This.

For the awk solution to make it work , use nawk or gawk and try.