Awk: How to get an awk variable out to the shell, using system() ?

I am reasonably capable with awk and its quirks, but not with shell weirdness. This has to be Bourne Shell for portability reasons. I have an awk program that is working just fine; it handles multiple input streams and produces several reports, based on the request (-v Variables). In addition to processing the stream, and producing the required report in the stream (so that it can be re-directed, etc), I write certain errors to a separate file.

Now I need to enhance the program, to write to a specific filename (constructed per execution, no problem), and to sort the contents.

I used the Site Search but nothing similar came up, due to the hundreds of badly formed thread titles.

The generic question may be as simple as the thread title.

This code snippet contains the seed of the problem, in simple form. The last two lines of code is a statement of intent; obviously it does not work as is.

BEGIN {
    ## constructed, simple form provided
    ERR_FILE =          "SERVER_yymmdd_Err.dat"
    ERR_FILE_TMP = "/tmp/SERVER_yymmdd_Err.dat"
    }

/Pattern/ {
    ## many of these; works fine
    if ( Foo != Bar ) printf Value_DMP, Foo, Bar > ERR_FILE_TMP
    }

END {
    printf "SERVER\t%s %s\n", SERVER, DATE > ERR_FILE
    ## need to get the filename out to the shell
    ## this states intention:
    system( "sort ERR_FILE_TMP >> ERR_FILE" )
    system( "rm   ERR_FILE_TMP" )
    }

Alternate approaches are welcome, as long as it does not result in a major code change.

You need to place the variable outside the double quotes, for example try:

system( "sort " ERR_FILE_TMP " >> " ERR_FILE )

That was so simple ! Thanks.

But I do not see how that is "outside the double quotes". For the benefit of the understanding of others, that is "concatenate the string before submitting it to system()". And my concern about informing the shell re awk vars, is a non-issue.

You're welcome. What I meant was, in your original code you had ERR_FILE_TMP and FILE_TMP inside the double quotes, so they would be interpreted by awk as a literal string instead of a variable. So yes, the way to procede then is to create the string to be passed to system() by concatenation of string parts and variables...