I use that notation at my log files:
[something-something]
somethings are necessary and variable length characters and numbers. My special log file includes line that has characters explained above. i.e.
Line 1: 14:40:45,107 DEBUG [AS-kmksdf] Error occured!
Line 2: something somethingg..
Line 3: 14:40:45,108 DEBUG Some other errors
I want to get Line 1 and Line 2(because error continues at line 2). My log files grows dynamically and I show it at my screen with:
tail -f log.txt
How can I show the lines that begins with my special line until it gets a new line that has number number : number number : number number , one or more length number (this is hour and comma seperated error code number)
(I think looking for number,number: may be enough)
I use cygwin with windows7 and can run some linux commands on my cmd.exe.
This question is releted to: this question
Best Answer
Command
How it works
awk
processes the piped output line by line.If the first field of the line (delimited by space and denoted by
$1
) matches the regular expression[0-9][0-9]:[0-9][0-9]:[0-9][0-9],[0-9]+
, the value ofMATCH
gets changed:If the pattern
\[[A-Za-z]+-[A-Za-z]+\]
occurs,MATCH
gets set to1
(truthy).If the pattern
\[[A-Za-z]+-[A-Za-z]+\]
does not occur,MATCH
gets set to0
(falsy).Finally, if
MATCH
is truthy,print $0
prints the entire line (denoted by$0
).Since
MATCH
is truthy if and only if the last line starting with a time stamp contained the pattern[something-something]
, this yields the desired output.