Quote:
Instead of taking input records one by one and applying STEP 1 and STEP 2 successively, is there a way to tell awk to apply the STEP 1 to all the input records, wait until they are all processed, and then apply the STEP 2?
|
I think this is one of those obvious-when-you-think-about-it answers: you output your first awk command to a temporary file, and run your second awk on the temporary file.
One has to ask
why would you do this? I assume that that 1 or 0 is going to be used by something else: if it is just for this process, then it is an unnecessary stage.
If you
must do this in two steps, within a single iteration of awk, I guess you could read the whole file into an array, and then process the array, possibly in an END section. But: keep it simple should be the whole philosophy of shell and awk work.
(awk has two special patterns: BEGIN and END. Whilst it works on a line by line basis, it executes stuff for the BEGIN pattern before it starts to read its input, and the END section after the line-by-line stuff is finished. You can use a BEGIN pattern in awk to write code that has no input: think awk "Hello World"!
By the way: NF is an awk built-in variable. If you try to use it for something else, I think confusion will ensue.