Chapter 4. Printing Output
One of the most common programming actions is to
print, or output, some or all of the input. Use
the print statement for simple output, and the
printf statement for fancier formatting. 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
and Section 4.4 later in this chapter.)
For printing with specifications, you need the
printf statement (see Section 4.5 later in this chapter).
Besides basic and formatted printing, this chapter
also covers I/O redirections to files and pipes, introduces the special
filenames that gawk processes internally, and
discusses the close built-in function.
The print Statement
The 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 are output, separated by
single spaces, followed by a newline. The statement looks like this:
item1,item2, ...
The entire list of items may be optionally enclosed in parentheses. The parentheses are necessary if any of the item expressions uses the
> relational operator; otherwise, it could be
confused with a redirection (see Section 4.6 later in this chapter).
The items to print can be constant strings or numbers, fields of the
current record (such as $1), variables, or any
awk
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access