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.

66 <strong>G<strong>AWK</strong></strong>: <strong>Effective</strong> <strong>AWK</strong> <strong>Programming</strong>In this case, the phone numbers had to be printed as strings because the numbers areseparated by a dash. Printing the phone numbers as numbers would have produced justthe first three digits: ‘555’. This would have been pretty confusing.It wasn’t necessary to specify a width for the phone numbers because they are last ontheir lines. They don’t need to have spaces after them.The table could be made to look even nicer by adding headings to the tops of thecolumns. This is done using the BEGIN pattern (see Section 6.1.4 [The BEGIN and ENDSpecial Patterns], page 99) so that the headers are only printed once, at the beginning ofthe awk program:awk ’BEGIN { print "Name Number"print "---- ------" }{ printf "%-10s %s\n", $1, $2 }’ BBS-listThe above example mixed print and printf statements in the same program. Usingjust printf statements can produce the same results:awk ’BEGIN { printf "%-10s %s\n", "Name", "Number"printf "%-10s %s\n", "----", "------" }{ printf "%-10s %s\n", $1, $2 }’ BBS-listPrinting each column heading with the same format specification used for the column elementsensures that the headings are aligned just like the columns.The fact that the same format specification is used three times can be emphasized bystoring it in a variable, like this:awk ’BEGIN { format = "%-10s %s\n"printf format, "Name", "Number"printf format, "----", "------" }{ printf format, $1, $2 }’ BBS-listAt this point, it would be a worthwhile exercise to use the printf statement to line upthe headings and table data for the ‘inventory-shipped’ example that was covered earlierin the section on the print statement (see Section 4.1 [The print Statement], page 58).4.6 Redirecting Output of print and printfSo far, the output from print and printf has gone to the standard output, usually theterminal. Both print and printf can also send their output to other places. This is calledredirection.A redirection appears after the print or printf statement. Redirections in awk arewritten just like redirections in shell commands, except that they are written inside the awkprogram.There are four forms of output redirection: output to a file, output appended to a file,output through a pipe to another command, and output to a coprocess. They are all shownfor the print statement, but they work identically for printf:print items > output-fileThis type of redirection prints the items into the output file named output-file.The file name output-file can be any expression. Its value is changed to a stringand then used as a file name (see Chapter 5 [Expressions], page 75).

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

Saved successfully!

Ooh no, something went wrong!