Previous Table of Contents Next

The printf command can send output to the same destinations as listed in the table describing the print command.

The printf is a much more sophisticated output statement. It generates formatted output. The general format of the printf statement is

     printf( format, expr 1, expr 2, ...,expr n )

The parentheses are not necessary. The format argument is always required. The format argument specifies how to format each of the expressions expr. Each specification begins with a % sign. For example,

     printf ("%-10s %5d %%%2s\n", $1, $2, $3 )

prints the first field left-justified in a 10-column output field; the second field is right-justified in a five-column output field; and the third field is left-justified in a two-column output field preceded by a percent sign.

The following table defines the formats available for use with the printf command.

Character Prints the Expression as

c A single ASCII character.
d A decimal integer.
e In exponential notation. The format is [-]d.ddddddE[+-]dd.
f In exponential notation. The format is [-]ddd.dddddd.
g Uses the shorter of e or f conversion and suppresses nonsignificant zeros.
o An unsigned octal number.
s A string of characters.
x An unsigned hexadecimal number.
% Prints the % sign. No related argument is needed.

Constants may be placed in the format string to print as themselves. Escape sequences may also be placed in the format argument. Refer to the section Escape Sequences for a list of valid escape sequences supported by the printf function.

Variable printf Format  The format part of the printf statement may be a constant or a variable. The ability to use a variable allows you to have variable formatting. For example,

     echo | nawk '{
     for ( i = 1 ; i <= 20; i++ ) {
         printf(format, i)

prints the number from 1 to 20 with 0 to 19 spaces preceding the number.

Closing Files and Pipes  Once you open a file or pipe for output writing, it remains open for further writing until you close it. Each file and pipe are referenced by unique expressions. For example, if you write to a file

     print > "mynawkoutfile"

then it remains open until you close it. To close the file you must refer to it by the exact same name. For example,


The closing of files is necessary if you write to a file, then want to read it in later in the same nawk program.

Control-Flow Statements

Nawk provides many of the control-flow statements found in the C programming language. It has the if-else statement for decision making tasks. The looping statements of while, for, and do are supported, as well as various other statements. The following table briefly defines the control-flow statements provided in nawk.

Statement Description

{ statements } Groups a set of single statements into a group. These braces are inside the action braces. For example,
/John/    { if ( $3 > "18" )
                    { print
                      adults++ }

The outside set of braces are action braces. The inside set are grouping braces. They force all the statements to be executed if the if statement is true. Normally only the first statement after the if is executed.

break Immediately exit present loop structure. For example,
i = 1
while ( i <= 100 )
 if ! ( i % 2 )
 else if ( Array[i] == "END" )

This loop counts from 1 to 100 and displays even numbers only. If the string "END" is encountered in the Array, then the loop is exited immediately.

continue Immediately start the next iteration of the enclosing for, while, or do loop.
do statement while (expression ) Execute the statement. If the expression is true, repeat the loop; if not, exit the loop.
exit Exit processing the pattern and action commands and process the END section.
exit expression Exit the nawk program and return the value of the expression as the program status code.
for ( expr 1; expr2; expr3) statement Set expr1, loop while expr2 is true, and perform expr3 before the next iteration of the loop is executed. For example,
for ( i = 1; i < 100; i++ )
      printf "%10d\n", i

prints the numbers from 1 to 100 in a 10-column output field.

for ( variable in array ) statement Execute statement for each element in the array with variable set to the next subscript. For example,
split("1 2 3 4 5", NUMS)
for ( N in NUMS )
   print NUMS[N]

prints the numbers from 1 through 5 from array NUMS.

if ( expression ) statement If expression is true, then execute statement. For example,
if ( $1 == 100 )

causes the nawk program to exit after reading the first 100 lines of input.

if( expression ) statement1 else statement2 If expression is true, execute statement1, else execute statement2
next Start the next iteration of the main input loop. The next input record is read in and processed immediately.
while ( expression ) statement If expression is true, execute the statement and repeat the loop. For example,
i = 100
 while ( i >= 1 )
  print array[i--]

prints the elements of an array in reverse order.

; The empty statement

Previous Table of Contents Next