21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 4: Printing Output 73If you use more files than the system allows you to have open, gawk attempts to multiplexthe available open files among your data files. gawk’s ability to do this depends upon thefacilities of your operating system, so it may not always work. It is therefore both goodpractice and good portability advice to always use close on your files when you are donewith them. In fact, if you are using a lot of pipes, it is essential that you close commandswhen done. For example, consider something like this:{}...command = ("grep " $1 " /some/file | my_prog -q " $3)while ((command | getline) > 0) {process output of command}# need close(command) hereThis example creates a new pipeline based on data in each record. Without the call toclose indicated in the comment, awk creates child processes to run the commands, until iteventually runs out of file descriptors for more pipelines.Even though each command has finished (as indicated by the end-of-file return statusfrom getline), the child process is not terminated; 2 more importantly, the file descriptorfor the pipe is not closed and released until close is called or awk exits.close will silently do nothing if given an argument that does not represent a file, pipeor coprocess that was opened with a redirection.Note also that ‘close(FILENAME)’ has no “magic” effects on the implicit loop that readsthrough the files named on the command line. It is, more likely, a close of a file that wasnever opened, so awk silently does nothing.When using the ‘|&’ operator to communicate with a coprocess, it is occasionally usefulto be able to close one end of the two-way pipe without closing the other. This is done bysupplying a second argument to close. As in any other call to close, the first argument isthe name of the command or special file used to start the coprocess. The second argumentshould be a string, with either of the values "to" or "from". Case does not matter. As thisis an advanced feature, a more complete discussion is delayed until Section 10.2 [Two-WayCommunications with Another Process], page 170, which discusses it in more detail andgives an example.Advanced Notes: Using close’s Return ValueIn many versions of Unix awk, the close function is actually a statement. It is a syntaxerror to try and use the return value from close:command = "..."command | getline inforetval = close(command)# syntax error in most Unix awksgawk treats close as a function. The return value is −1 if the argument names somethingthat was never opened with a redirection, or if there is a system problem closing the file2 The technical terminology is rather morbid. The finished child is called a “zombie,” and cleaning upafter it is referred to as “reaping.”

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!