21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

58 <strong>G<strong>AWK</strong></strong>: <strong>Effective</strong> <strong>AWK</strong> <strong>Programming</strong>4 Printing OutputOne of the most common programming actions is to print, or output, some or all of theinput. Use the print statement for simple output, and the printf statement for fancierformatting. The print statement is not limited when computing which values to print.However, with two exceptions, you cannot specify how to print them—how many columns,whether to use exponential notation or not, and so on. (For the exceptions, see Section 4.3[Output Separators], page 60, and Section 4.4 [Controlling Numeric Output with print],page 60.) For printing with specifications, you need the printf statement (see Section 4.5[Using printf Statements for Fancier Printing], page 61).Besides basic and formatted printing, this chapter also covers I/O redirections to filesand pipes, introduces the special file names that gawk processes internally, and discussesthe close built-in function.4.1 The print StatementThe print statement is used to produce output with simple, standardized formatting.Specify only the strings or numbers to print, in a list separated by commas. They areoutput, separated by single spaces, followed by a newline. The statement looks like this:print item1, item2, ...The entire list of items may be optionally enclosed in parentheses. The parentheses arenecessary if any of the item expressions uses the ‘>’ relational operator; otherwise it couldbe confused with a redirection (see Section 4.6 [Redirecting Output of print and printf],page 66).The items to print can be constant strings or numbers, fields of the current record (suchas $1), variables, or any awk expression. Numeric values are converted to strings and thenprinted.The simple statement ‘print’ with no items is equivalent to ‘print $0’: it prints theentire current record. To print a blank line, use ‘print ""’, where "" is the empty string.To print a fixed piece of text, use a string constant, such as "Don’t Panic", as one item.If you forget to use the double-quote characters, your text is taken as an awk expression,and you will probably get an error. Keep in mind that a space is printed between any twoitems.4.2 Examples of print StatementsEach print statement makes at least one line of output. However, it isn’t limited to onlyone line. If an item value is a string that contains a newline, the newline is output alongwith the rest of the string. A single print statement can make any number of lines thisway.The following is an example of printing a string that contains embedded newlines (the‘\n’ is an escape sequence, used to represent the newline character; see Section 2.2 [EscapeSequences], page 25):$ awk ’BEGIN { print "line one\nline two\nline three" }’⊣ line one⊣ line two

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

Saved successfully!

Ooh no, something went wrong!