0

以下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。

4

1 回答 1

1

只需尝试添加>counter".txt"到您的打印语句中。例如:

printf "Thread dump #%d\n", counter >counter".txt";

如上所述,每个打印语句都应附加此字符串。然后尝试运行 awk 脚本。

某些版本的 unix 不支持这种语法。你有没有在行尾加分号?

在这种情况下,请执行以下操作:

file=counter".txt";
    printf "Thread dump #%d\n", counter >file;
于 2013-01-23T10:23:58.803 回答