Skip to main content
3 of 4
added 145 characters in body
John1024
  • 76.4k
  • 12
  • 176
  • 165

To summarize, you want to print lines starting with the line number that you specify and continuing until just before the first following line that starts with a date. In your example, the starting line is 3. In that case:

$ awk '{if (NR==3)f=1; else if (/^[0-9-]{10} /)f=0} f{print}' trace.log
2016-10-07 15:49:07,537 ERROR Some exception
 stacktrace line 1
 stacktrace line 2
 .
 .
 stacktrace line n

The above code works as follows:

  • if (NR==3)f=1

    On the line number you specify, set variable f to one.

  • else if (/^[0-9-]{10} /)f=0

    On other lines, set f to zero if the line starts with 10 characters that are digits or dashes followed by a space. In other words, set f to zero on the first line that starts with something that looks like a date.

    If need be, we can get use more complex regexes to identify the start of a date. For example, the following requires that the line start with something that looks like a data, followed by a space, followed by something that looks like time, followed by a comma.

      awk '{if (NR==3)f=1; else if (/^[0-9-]{10} [0-9:]{8},/)f=0} f{print}' trace.log
    

    Still further improvements on this are possible.

  • f{print}

    If f is nonzero, print the line.

    For brevity, we could replace f{print} with just f. This is possible because, when an action is not specified explicitly, the default action of print is used.

John1024
  • 76.4k
  • 12
  • 176
  • 165