syntax problem grepping?

I am calculating a time and appending a space in front of it to get only certain records in a file because the times are represented in HH:II:SS format and I don't want to see anything other than the actual hour and minute combination (hence appending the space to the front of the time). My variable line is:

 GREPTIME2="' "$GREPTIME"'"

and this displays correctly on an echo:

 [131]\+echo GREPTIME2 = ' 06:38' <<=== \(this is from a trace\).

When I issue my grep statement, here's my syntax:

 COUNT=\`grep "$GREPTIME2" "$LBFILE" \`

When I execute the script, here's what shows up in the trace:

 [103]\+[103]\+grep ' 06:45' /mydata.log

COUNT=

Note that COUNT is equal to nothing. If I copy and paste the part of the previous statement:

 grep ' 06:45' /mydata.log

and execute it from the command line, I get 135 lines (which is what I expect to get)! What am I doing wrong? I can grep from the command line and find it, but it doesn't work in the script! Help!!!

Try

GREPTIME2=" $GREPTIME"

So your problem here is to populate the COUNT variable with the no. of occurence of the string.

Included the count option in your grep statement

COUNT=`grep -c "$GREPTIME2" "$LBFILE" `

I don't think either of the repliers understands the problem.

1) The variable actually contains the correct syntax of what I want (as seen in the trace).

2) When the grep command is executed, I am expecting to get results - I get NONE. This is where the problem is.

3) I don't need to (although I was unaware of a -c option) to use that to get a count because I am piping it to a wc -l which will give me the count (I just didn't put that in my statement as it would only confuse the issue).

As stated earlier, the grep command is NOT returning anything when executed in the script, but IS returning something when run from the command line. What is wrong?

Actually, your trace shows that you are searching for the time surrounded by single quotes. I think you want to search for the time without the quotes.

Which is exactly why I suggested my change. Your grep is not searching for the string you think it is.

Both, Kahuna and Lorcan are right.
Your statement should be changed to:

GREPTIME2=" $GREPTIME"

And the '-c' option in the 'grep' should be used.

Dsimpg1, the entire scenario should always be displayed to get the a full
analisys of the problem -- the solution could be in the hidden statement.

Perhaps I am missing something, but if you'll notice, there IS a space before the quotes.

Again, there IS a space in there (as seen in both traces).

I am not understanding why it is NOT finding anything (doesn't find anything even if there is no space via the script, but DOES find it if I run it via the command line). Any ideas?

Dsimpg1,
We have done our best to explain it to you that the way you have it,
it does not work.

The reason is that your variable <GREPTIME2> has single quotes
surrounding it, thus the 'grep' is not doing what you want.

I suggest you make the recommended changes and test it.

You will realize that our solution works.

Perhaps an example would better explain what is happening

$cat example
VAR="abc"
VAR1="' "$VAR"'"
VAR2=" $VAR"
echo "VAR1=$VAR1"
echo "VAR2=$VAR2"
echo "data for VAR1 abcdef" |grep "$VAR1" 
echo "data for VAR2 abcdef" |grep "$VAR2" 

$ksh example
VAR1=' abc'
VAR2= abc
data for VAR2 abcdef

Your grep is looking for a single quote, a space, the date, and another single quote. Your grep does not look for a space followed by the date.

My apologies to all for not understanding what it is you were trying to say. Yes, you were correct, I was searching for it with quotes. Now that I have done what you said and removed the conversion to GREPTIME2 (I am only using GREPTIME now), it works as it should. Guess I tried to make something a little more complicated than it needed to be. Again, sorry. Thanks for all of your help!