Skip to main content
1 of 4
John1024
  • 76.4k
  • 12
  • 176
  • 165

In your example, all the stacktrace lines were indented by a space. If that is true of your real log file, then to print all lines starting from the line number you specify (3 in your example) but stopping with the first non-indented line, try:

$ awk '{if (NR==3)f=1; else if (!/^ /)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 (!/^ /)f=0

    On other lines, set f to zero is the line is not indented.

  • f{print}

    If f is nonzero, print the line.

John1024
  • 76.4k
  • 12
  • 176
  • 165