Printing Output

Output separators

OFS The output field separator, a space by default.
ORS The output record separator, by default a newline.
awk 'BEGIN { OFS = ";"; ORS = "\n\n" } { print $1, $2 }' mail-list

Numeric output with print

OFMT The output format for numbers, “%.6g”, by default.

awk 'BEGIN {
OFMT = "%.0f"
print 17.23, 17.54 }'

printf Statements

printf format, item1, item2, …

awk 'BEGIN {
ORS = "\nOUCH!\n"; OFS = "+"
msg = "Don\47t Panic!\nBe cool!"
printf "%s\n", msg
print
print msg
}'

Format-control letters

%c

Print a number as a character

%d , %i

Print a decimal integer

%e , %E

Print a number in scientific (exponential) notation

%f

Print a number in floating-point notation

%s

Print a string

awk 'BEGIN { printf "%s%c%s\n", "Don", 39, "t Panic" }'

awk 'BEGIN { printf "%s\n", "string"
printf "%4.3e\n", 1950
printf "%4.3f\n", 1950
printf "%c\n", 65
}'

awk 'BEGIN { FS = ":" } $3 > 999 {
printf "Name: %s\nUID: %d\nShell: %s\n", $1, $3, $7
}' /etc/passwd

Modifiers for printf Formats

  • N$ positional specifier

awk 'BEGIN { printf "%3$s%2$c%1$s\n", "t Panic", 39, "Don"}'
  • (-)width width modifier

awk 'BEGIN { printf "%4s", "foo" }'
awk 'BEGIN { printf "%6s", "foo" }'
awk 'BEGIN { printf "%-6s", "foo" }'
awk 'BEGIN { printf "%-4s", "foo" }'
  • .number maximum number of characters from the string

awk 'BEGIN { s = "ABCDEFG" ; printf "%5.3s\n", s }'
   ABC

Examples using printf

awk '(NR <= 1) || (NR >= 11) { printf "%10s %s\n", $1, $2 }' mail-list
    Amelia 555-5553
 Jean-Paul 555-2127

awk '(NR <= 1) || (NR >= 11) { printf "%-10s %s\n", $1, $2 }' mail-list
Amelia     555-5553
Jean-Paul  555-2127

awk 'BEGIN { format = "%-10s %s\n"
printf format, "Name", "Number"
printf format, "----", "------"
}
(NR <= 1) || (NR >= 11) {
printf format, $1, $2
}' mail-list
Name       Number
----       ------
Amelia     555-5553
Jean-Paul  555-2127

awk 'BEGIN { "cat mail-list | wc -l" | getline num_line
close("cat mail-list | wc -l")
format = "%-10s %s\n"
printf format, "Name", "Number"
printf format, "----", "------" }
(NR <= 1) || (NR >= num_line) {
printf format, $1, $2 }' mail-list

Redirecting output

print items > output-file

awk '{ print $2 > "phone-list"
print $1 > "name-list"
}' mail-list

print items >> output-file

awk 'BEGIN { "cat mail-list | wc -l" | getline num_line
close("cat mail-list | wc -l")}
(NR <= 1) || (NR >= num_line) {
print $2 >> "phone-list" }' mail-list

print items | command

awk '{ command = "sort -r"
print $1 | command }' mail-list
print items |& command
command is a coprocess.

Piping into sh

ls -1 | awk '$0 ~ /[A-Z]+/ \
{ printf "mv %s %s\n", $0, tolower($0) | "sh" }
END { close("sh") }'

Special files for standard preopened data streams

/dev/tty

represents the “terminal”

/dev/stdin

standard input

/dev/stdout

standard output

/dev/stderr

standard error

/dev/fd/N

file descriptor N

Network communications

/net-type/protocol/local-port/remote-host/remote-port
Used with the ‘ |& ’ operator for communicating with a coprocess.

Closing input and output redirections

close(filename)
close(command)
Its value must exactly match the string that was used to open the file or start the command.
For coprocesses, it is possible to close only one direction of the communications.
awk '{ command = "sort -r"
print $1 | command }
END { close(command) }' mail-list