Skip to main content
added 221 characters in body
Source Link
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.

Alternative

Some versions of awk don't support repetition factors like {10}. If that is the case on your system, try:

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

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.

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.

Alternative

Some versions of awk don't support repetition factors like {10}. If that is the case on your system, try:

awk '{if (NR==3)f=1; else if (/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] /)f=0} f{print}' trace.log
added 145 characters in body
Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165

With the line number of the exception known

In your example, all the stacktrace lines were indented by a space. If that is true of your real log fileTo summarize, thenyou want to print all lines starting fromwith the line number that you specify (3 in your example) but stopping withand continuing until just before the first non-indentedfollowing line that starts with a date. In your example, trythe 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 isif the line is not indentedstarts 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.

Printing all exceptions regardless of line number

In this version, we print the all indented lines and the line before the indented lines:

$ awk 'last && /^ /{print last; last=""} !/^ /{last=$0} /^ /' trace.log
2016-10-07 15:49:07,537 ERROR Some exception
 stacktrace line 1
 stacktrace line 2
 .
 .
 stacktrace line n

With the line number of the exception known

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.

Printing all exceptions regardless of line number

In this version, we print the all indented lines and the line before the indented lines:

$ awk 'last && /^ /{print last; last=""} !/^ /{last=$0} /^ /' trace.log
2016-10-07 15:49:07,537 ERROR Some exception
 stacktrace line 1
 stacktrace line 2
 .
 .
 stacktrace line n

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.

added 419 characters in body
Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165

With the line number of the exception known

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.

Printing all exceptions regardless of line number

In this version, we print the all indented lines and the line before the indented lines:

$ awk 'last && /^ /{print last; last=""} !/^ /{last=$0} /^ /' trace.log
2016-10-07 15:49:07,537 ERROR Some exception
 stacktrace line 1
 stacktrace line 2
 .
 .
 stacktrace line n

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.

With the line number of the exception known

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.

Printing all exceptions regardless of line number

In this version, we print the all indented lines and the line before the indented lines:

$ awk 'last && /^ /{print last; last=""} !/^ /{last=$0} /^ /' trace.log
2016-10-07 15:49:07,537 ERROR Some exception
 stacktrace line 1
 stacktrace line 2
 .
 .
 stacktrace line n
Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165
Loading