以下AWK
代码从日志文件中提取 Java 线程转储:
# First thread dump line
/^Full thread dump Java/ {
thread_dump=1;
counter++;
printf "Thread dump #%d\n", counter
}
# Last thread dump text block
/^Heap[:space:]*$/ {
thread_dump=2;
}
# Last thread dump line
{ if (thread_dump==2 && $0 ~ "^[:space:]*$") {
thread_dump=0;
printf "End of Thread dump #%d\n\n", counter;
}
}
# Print line only if in thread dump block
{ if (thread_dump!=0) {print $0 } }
结果awk -f extract.awk log.out
是这样的:
Thread dump #1
Full thread dump Java HotSpot(TM) 64-Bit Server VM (20.12-b01 mixed mode):
...
End of Thread dump #1
Thread dump #2
Full thread dump Java HotSpot(TM) 64-Bit Server VM (20.12-b01 mixed mode):
...
End of Thread dump #2
我想将每个线程转储写入一个单独的文件。文件名应该包括一些数据,比如日期和连续 ID,比如thread_dump_002_2013_01_23_14_15
.
如何将print
命令重定向到格式化的文件名?
更新:
以下作品:
print $0 >"some_file_name.txt"
但是,以下内容:
print $0 > counter".txt"
返回以下错误:
awk: syntax error at source line 25 source file extract.awk
context is
{ if (thread_dump!=0) { print $0 >>> >counter".txt" <<< } }
awk: illegal statement at source line 26 source file extract.awk
PS:我在mac上使用AWK。