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!!!
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.
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?
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!