-->

Previous | Table of Contents | Next

Page 571

{ while ("who -u" | getline)

  {

      # process each line from the who command

   }

}

The who command is executed once and each of its output lines is processed by getline. You could also use the form "command" | getline variable.

Ending Input from a File or Command

Whenever you use getline to get input from a specified file or command, you should close it when you are done processing the data. There is a maximum number of open files allowed to awk that varies with operating system version or individual account configuration (a command output pipe counts as a file). By closing files when you are done with them, you reduce the chances of hitting the limit.

The syntax to close a file is simply

close ("filename")

where filename is the one specified on the getline (which could also be stdin, a variable that contains the filename, or the exact command used with getline).

Output

There are a few advanced features for output: pretty formatting, sending output to files, and piping output as input to other commands. The printf command is used for pretty formatting—instead of seeing the output in whatever default format awk decides to use (which is often ugly), you can specify how it looks.

printf

The print statement produces simple output for you. If you want to be able to format the data (producing fixed columns, for instance), you need to use printf. The nice thing about awk printf is that it uses syntax that is very similar to the printf() function in C.

The general format of the awk printf is as follows (the parentheses are only required if a relational expression is included):

printf format-specifier, variable1,variable2, variable3,..variablen

printf(format-specifier, variable1,variable2, variable3,..variablen)

Personally, I use the second form because I am so used to coding in C.

The variables are optional, but format-specifier is mandatory. Often you will have printf statements that only include format-specifier (to print messages that contain no variables):

printf ("Program Starting\n")

printf ("\f")        # new page in output

format-specifier can consist of text, escaped characters, or actual print specifiers. A print specifier begins with the percent sign (%), followed by an optional numeric value that specifies the

Page 572

size of the field, then the format type follows (which describes the type of variable or output format). If you want to print a percent sign in your output, you use %%.

The field size can consist of two numbers separated by a decimal point (.). For floating-point numbers, the first number is the size of the entire field (including the decimal point); the second number is the number of digits to the right of the decimal. For other types of fields, the first number is the minimum field size and the second number is the maximum field size (number of characters to actually print); if you omit the first number, it takes the value of the maximum field size.

The print specifiers determine how the variable is printed; there are also modifiers that change the behavior of the specifiers. Table 27.9 shows the print format specifiers.

Table 27.9. Format specifiers for awk.

Format Meaning
%c ASCII character
%d An integer (decimal number)
%i An integer, just like %d
%e A floating-point number using scientific notation (1.00000E+01)
%f A floating-point number (10.43)
%g awk chooses between %e or %f display format (whichever is shorter)
suppressing nonsignificant zeros.
%o An unsigned octal (base 8) number (integer)
%s A string of characters
%x An unsigned hexadecimal (base 16) number (integer)
%X Same as %x but using ABCDEF instead of abcdef
NOTE
If you attempt to print a numeric value or variable using %c, it will be printed as a character (the ASCII character for that value will print).

The format modifiers change the default behavior of the format specifiers. Listing 27.4 shows the use of various specifiers and modifiers.

Page 573

Listing 27.4. printf format specifiers and modifiers.

printf("%d %3.3d %03.3d %.3d %-.3d %3d %-3d\n", 64, 64, 64, 64, 64, 64, 64)

printf("%c %c %2.2c %-2.2c %2c %-2c\n", 64, "abc", "abc", "abc", "abc", "abc")

printf("%s %2s %-2s %2.2s %-2.2s %.2s %-.2s\n",

       "abc", "abc", "abc", "abc", "abc", "abc", "abc")

printf("%f %6.1f %06.1f %.1f %-.1f %6f\n",

       123.456, 123.456, 123.456, 123.456, 123.456, 123.456)



64 064 064 064 064  64 64

@ a  a a   a a

abc abc abc ab ab ab ab

123.456000  123.5 0123.5 123.5 123.5 123.456000

When using the integer or decimal (%d) specifier, the field size defaults to the size of the value being printed (2 digits for the value 64). If you specify a field maximum size that is larger than that, you automatically get the field zero filled. All numeric fields are right-justified unless you use the minus sign (-) modifier, which causes them to be left-justified. If you specify only the field minimum size and want the rest of the field zero filled, you have to use the zero modifier (before the field minimum size).

When using the character (%c) specifier, only one character prints from the input no matter what size you use for the field minimum or maximum sizes and no matter how many characters are in the value being printed. Note that the value 64 printed as a character shows up as @.

When using the string (%s) specifier, the entire string prints unless you specify the field maximum size. By default, strings are left-justified unless you use the minus sign (-) modifier, which causes them to be right-justified.

When using the floating (%f) specifier, the field size defaults .6 (as many digits to the left of the decimal and 6 digits to the right). If you specify a number after the decimal in the format, that many digits will print to the right of the decimal and awk will round the number. All numeric fields are right-justified unless you use the minus sign (-) modifier, which causes them to be left-justified. If you want the field zero filled, you have to use the zero modifier (before the field minimum size).

The best way to determine printing results is to work with it. Try out the various modifiers and see what makes your output look best.

Output to a File

You can send your output (from print or printf) to a file. The following creates a new (or empties out an existing) file containing the printed message:

printf ("hello world\n") > "datafile"

If you execute this statement multiple times or other statements that redirect output to datafile, the output will remain in the file. The file creation/emptying out only occurs the first time the file is used in the program.

Previous | Table of Contents | Next