次の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 を使用しています。