I had to read that script once or twice to make sure I got it straight in my head. It's a little confusing because we see the END at the beginning.
$0
is the entire line.
The line /^[0-9]/
matches the process id (specifically) and that block
then sets the sentinel variable f to 0.
The block starting with /INET /
matches (and then strips, via the sub(..))
the open port number. The sentinel value f is set to 1 so that we know to
print differently when we hit the END
. Each time we finish an output
collection (ie, the entire output from pfiles for a process), we hit the END
block and print the output.
BTW, the RS
is the Record Separator.
Running the script on just one process might make it a little easier to get the head around it.
Sorry, forgot to answer your other question re the redirection.
2>&-
in this context means "redirect stderr from the process to standard input",
so that nawk
takes input from there rather than a file.