문제

The following AWK code extracts Java thread dump from a log file:

# 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 } }

The result of awk -f extract.awk log.out is something like:

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

I would like to write each thread dump to a separate file. The file names should be include some data like date and consecutive id, something like thread_dump_002_2013_01_23_14_15.

How do I redirect a print command to a formatted filename?

Update:

The following works:

print $0 >"some_file_name.txt"

However, the following:

print $0 > counter".txt"

Returns the following error:

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: I'm using AWK on mac.

도움이 되었습니까?

해결책

Just try adding >counter".txt" to your print statements. for example:

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

Every print statement should be appended with this string as mentioned above. And then try running the awk script.

Some flavours of unix do not support this syntax. Did you put a semi colon at the end of the line or not?

In such cases do this:

file=counter".txt";
    printf "Thread dump #%d\n", counter >file;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top