Help using SYSTEM function in NAWK

I am scanning a file (line by line) for format errors. A line could have multiple errors. Each field in the line is evaluated for errors and sent, along w/ any error messages, to a temporary file. Finally, if any errors were detected, this temporary file is then appended to the errorFile. The tmpFile is created containing all messages, but the errorFile is not created! Any ideas?

#!/usr/bin/bash
TMPFILE="tmpFile"
ERRFILE="errorFile"
nawk -v TMP=$TMPFILE -v ERR=$ERRFILE '{
  ... scan each field for errors and increment "error" as necessary
  print $1, "Error message ..." >> TMP
  print $2, "Error message ..." >> TMP
  ...
  if (error>0) system("cp TMP ERR")
}' xx.txt

I'm not sure if I understand your problem correctly. If not, please clarify.
I think that you are having problem with following cp command ran by system:

system("cp TMP ERR")

And you are expecting a file named "errorFile" to be created based on content of tmpFile.
If it is so, then problem is following:
argument to system command is being passed as string (In double quotes).HereTMP and ERR won't be treated as variable but just it's literal value.
And I guess there is no file with name "TMP", so cp will fail.
Even if there is a file called "TMP", ERROR file will be created with name "ERR".

You may try like below:

cmd="cp "TMP" "ERR;
if (error>0) system(cmd)

That worked. Thank you.

In your requirements, you stated:

and the proposed to the system() call was:

While correct, you do realize that man cp (linux) overwrites the errorFile with tempFile -- it does not append. May I suggest:

#!/usr/bin/bash
ERRFILE="errorFile"

nawk -v ERR=$ERRFILE '{
  ... scan each field for errors
  print $1, "Error message ..." >> ERR
  print $2, "Error message ..." >> ERR
  ...
}' xx.txt

This will append the errors to errorFile, which is only created if an error is found.

I am new to this forum, but not new to forums in general. It is refreshing that others are watching my back. Thanks.

I later realized that the cp would overwrite my file so I changed it to "cat" using the same format. The TMP file is being used to hold a intermediate report on the current record in question. This report potentially contains both errors and correct information, but I do not know until verifying the last field. If all is clean, the script loops back and reviews the next record.

Again, thank you for your advice.