Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I use that notation at my log files:


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

share|improve this question


tail -f log.txt | awk '{if($1~/[0-9][0-9]:[0-9][0-9]:[0-9][0-9],[0-9]+/){if($0~/\[[A-Za-z]+-[A-Za-z]+\]/){MATCH=1}else{MATCH=0}}if(MATCH){print $0}}'

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 of MATCH gets changed:

    • If the pattern \[[A-Za-z]+-[A-Za-z]+\] occurs, MATCH gets set to 1 (truthy).

    • If the pattern \[[A-Za-z]+-[A-Za-z]+\] does not occur, MATCH gets set to 0 (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.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .