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.

68 <strong>G<strong>AWK</strong></strong>: <strong>Effective</strong> <strong>AWK</strong> <strong>Programming</strong>The message is built using string concatenation and saved in the variable m.It’s then sent down the pipeline to the mail program. (The parentheses groupthe items to concatenate—see Section 5.6 [String Concatenation], page 82.)The close function is called here because it’s a good idea to close the pipe assoon as all the intended output has been sent to it. See Section 4.8 [ClosingInput and Output Redirections], page 71, for more information.This example also illustrates the use of a variable to represent a file orcommand—it is not necessary to always use a string constant. Using a variableis generally a good idea, because (if you mean to refer to that same file orcommand) awk requires that the string value be spelled identically every time.print items |& commandThis type of redirection prints the items to the input of command. The differencebetween this and the single-‘|’ redirection is that the output from commandcan be read with getline. Thus command is a coprocess, which workstogether with, but subsidiary to, the awk program.This feature is a gawk extension, and is not available in POSIX awk. SeeSection 3.8.7 [Using getline from a Coprocess], page 56, for a brief discussion.See Section 10.2 [Two-Way Communications with Another Process], page 170,for a more complete discussion.Redirecting output using ‘>’, ‘>>’, ‘|’, or ‘|&’ asks the system to open a file, pipe, orcoprocess only if the particular file or command you specify has not already been writtento by your program or if it has been closed since it was last written to.It is a common error to use ‘>’ redirection for the first print to a file, and then to use‘>>’ for subsequent output:# clear the fileprint "Don’t panic" > "guide.txt"...# appendprint "Avoid improbability generators" >> "guide.txt"This is indeed how redirections must be used from the shell. But in awk, it isn’t necessary.In this kind of case, a program should use ‘>’ for all the print statements, since the outputfile is only opened once. (It happens that if you mix ‘>’ and ‘>>’ that output is produced inthe expected order. However, mixing the operators for the same file is definitely poor style,and is confusing to readers of your program.)As mentioned earlier (see Section 3.8.9 [Points to Remember About getline], page 56),many awk implementations limit the number of pipelines that an awk program may haveopen to just one! In gawk, there is no such limit. gawk allows a program to open as manypipelines as the underlying operating system permits.Advanced Notes: Piping into shA particularly powerful way to use redirection is to build command lines and pipe theminto the shell, sh. For example, suppose you have a list of files brought over from a systemwhere all the file names are stored in uppercase, and you wish to rename them to havenames in all lowercase. The following program is both simple and efficient:

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

Saved successfully!

Ooh no, something went wrong!