It is not an incorrect entry, grep did exactly what it was designed for, you did a
grep "09:49.*yellow" out.log
and so 05:09:49. matches! The true issue is you not giving a better pattern, and here we cannot help you since you have not submitted any sample so we can see what you are up to, Scrutinsers submission is pefectly valid with the little info you gave so far (even if I guessed where the issues would be...) so next time I see a post from you like above, be sure I will close the thread a give you a warning.
True. In fact it will also match "09:49blah-blubb-foo-bar-whatnot-yellow" because ".*" in regexps means "any number of any character(s)" and should be used only with extreme caution.
This regexp is - most likely, because lacking any documentation of the problem we are left to guessing - completely screwed from the beginning and perhaps never did what it should do.
I wish to grep for time and a string in a log file.
The time to grep for is in this format hh:mm with a 24 hrs clock cycle.
The log file has the time in this format hh:mm:ss
The search should yield only those lines that matches both the string as well as any time between the grepped time + 3 mins.
For example: grep for time [23:59] and string [yellow] should yield all the lines that contain yellow as well as any time between 23:59:00 to 00:02:00 i.e 23:59 +3 mins
There is no need to open a new thread...
grep and pattern: grep will match whatever pattern you give, you have to find a way to restrict the output, that is don by analyzing more thinly what you want:
As you have seen Bakunin answer, one way to do this when looking for date/time is :
Do I have a space ( or 2 ) before my pattern, if so, include it in the pattern delimited by ", that may be enough...
But you havent given a complet line as sample for us to figure out... So we can only guess and I hate to reply to guesses I already have alot to do...
@Thread-O/P: Please, if you are asked to clarify your question, do not open a new thread but simply provide the information asked for. It happens to all of us sometimes to forget some vital data (or not to recognize it as indeed vital). The simple solution is to submit it after being asked for. If you open a new thread you are taking in fact away context because many people will not notice the connection between the two threads.
Here is just an example: I need to grep for time [23:59] and string [yellow]. It should yield all the lines that contain yellow as well as any time between 23:59:00 to 00:02:00 i.e 23:59 +3 mins.
The grep query should be smart enough to search for all timing between the given time + 3 mins on a 24 hrs clock.
So, in this case if my log file contains the below five lines
yellow team won at 00:23:59 on monday
red team won at 00:00:43 on monday
yellow team won at 00:01:07 on monday
yellow team won at 00:02:55 on monday
yellow team won at 23:59:23 on monday
Desired output should be
yellow team won at 00:01:07 on monday
yellow team won at 23:59:23 on monday
Thus my query should be generic for all timings and not just for 23:59.
The thing that is complex here is not the query; it is trying to figure out what you're trying to match. The English term "between" (from your requirement in red above) is ambiguous as to whether or not the end points are included. And, since you aren't including the seconds in your starting time, but the seconds value in the end point of your search seems to matter it is even more confusing. When you say:
are you referring to the three minutes and one second starting with 23:59:00 up to and including 00:02:00, the three minutes starting with 23:59:00 up to but not including 00:02:00, or the two minutes and 59 seconds starting with 23:59:01 up to but not including 00:02:00?
And, since the RE that you showed us in the original posting in these threads was 23:59.*yellow (which will only match if yellow comes AFTER the timestamp), but your sample input in message #10 in this thread always shows the color BEFORE the timestamp; is the code you want supposed to find the color after a matching timestamp; before a matching timestamp; or anywhere on the line that contains a matching timestamp?
I realize that these seem like picky details; but after more than 200 posts in these forums you should know by now that if the details of your request are not clear, the likelihood of getting working code depends on us guessing at what your real requirements are.
And, you said the time can vary. Will the script you want always only look for yellow , or is the color a parameter to your script as well?
If you had given us clear requirements for your search and sample contents of your file ( out.log ) in your original posting, we wouldn't still be trying to figure out what you want two days later. Help us help you by giving us a clear set of requirements!
You still didn't answer some of my questions, so making some assumptions, the following seems to do what I think you want. Although written using ksh, it has been tested with both bash and ksh:
#!/bin/ksh
IAm=${0##*/}
if [ $# -ne 1 ]
then printf "Usage: %s starting_HH:MM\n" "$IAm" >&2
exit 1
fi
# Verification that $1 is in proper format is left as an excercise for the user
HH=${1%:*} # Get Hour from $1
MM=${1#*:} # Get Minute from $1
HH=${HH#0} # Strip HH leading 0
MM=${MM#0} # Strip MM leading 0
M1=$(( (MM + 1) % 60)) # Calculate MM+1
M2=$(( (MM + 2) % 60)) # Calculate MM+2
H1=$(( (HH + (M1 < MM)) % 24)) # Calc HH for MM+1
H2=$(( (HH + (M2 < MM)) % 24)) # Calc HH for MM+2
HM=$(printf "%02d:%02d" "$HH" "$MM") # Format HH:MM
HM1=$(printf "%02d:%02d" "$H1" "$M1") # Format HH:MM for MM+1
HM2=$(printf "%02d:%02d" "$H2" "$M2") # Format HH:MM for MM+2
T0ERE=$(printf "%s:0[1-9]|%s:[1-5][0-9]" "$HM" "$HM") # ERE for MM
T12ERE=$(printf "(%s|%s):[0-5][0-9]" "$HM1" "$HM2") # ERE for MM+1 and MM+2
TERE=$(printf "(%s|%s)" "$T0ERE" "$T12ERE") # ERE for MM to MM+2
# final ERE for color before or after desired timestamps
ERE=$(printf "(yellow.*%s|%s.*yellow)" "$TERE" "$TERE")
grep -E "$ERE" out.log
When out.log contains:
yellow team won at 00:23:59 on monday
red team won at 00:00:43 on monday
yellow team won at 00:01:07 on monday
yellow team won at 00:02:55 on monday
yellow team won at 23:59:23 on monday
at 00:23:59 on monday, yellow team won
at 00:00:43 on monday, red team won
at 00:01:07 on monday, yellow team won
at 00:02:55 on monday, yellow team won
at 23:59:23 on monday, yellow team won
and the above script is invoked as:
./script_name 23:59
the output produced is:
yellow team won at 00:01:07 on monday
yellow team won at 23:59:23 on monday
at 00:01:07 on monday, yellow team won
at 23:59:23 on monday, yellow team won