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=1On the line number you specify, set variable
fto one.else if (/^[0-9-]{10} /)f=0On other lines, set
fto zero if the line starts with 10 characters that are digits or dashes followed by a space. In other words, setfto 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.logStill further improvements on this are possible.
f{print}If
fis nonzero, print the line.For brevity, we could replace
f{print}with justf. This is possible because, when an action is not specified explicitly, the default action ofprintis used.