Go forward to Output Separators.
Go backward to Print.
Go up to Printing.
Examples of `print' Statements
==============================
Here is an example of printing a string that contains embedded
newlines:
awk 'BEGIN { print "line one\nline two\nline three" }'
produces output like this:
line one
line two
line three
Here is an example that prints the first two fields of each input
record, with a space between them:
awk '{ print $1, $2 }' inventory-shipped
Its output looks like this:
Jan 13
Feb 15
Mar 15
...
A common mistake in using the `print' statement is to omit the comma
between two items. This often has the effect of making the items run
together in the output, with no space. The reason for this is that
juxtaposing two string expressions in `awk' means to concatenate them.
For example, without the comma:
awk '{ print $1 $2 }' inventory-shipped
prints:
Jan13
Feb15
Mar15
...
Neither example's output makes much sense to someone unfamiliar with
the file `inventory-shipped'. A heading line at the beginning would
make it clearer. Let's add some headings to our table of months (`$1')
and green crates shipped (`$2'). We do this using the `BEGIN' pattern
(see `BEGIN' and `END' Special Patterns: BEGIN/END.) to force the
headings to be printed only once:
awk 'BEGIN { print "Month Crates"
print "----- ------" }
{ print $1, $2 }' inventory-shipped
Did you already guess what happens? This program prints the following:
Month Crates
----- ------
Jan 13
Feb 15
Mar 15
...
The headings and the table data don't line up! We can fix this by
printing some spaces between the two fields:
awk 'BEGIN { print "Month Crates"
print "----- ------" }
{ print $1, " ", $2 }' inventory-shipped
You can imagine that this way of lining up columns can get pretty
complicated when you have many columns to fix. Counting spaces for two
or three columns can be simple, but more than this and you can get
"lost" quite easily. This is why the `printf' statement was created
(see Using `printf' Statements for Fancier Printing: Printf.); one of
its specialties is lining up columns of data.