11.12. Output Redirections

 < Day Day Up > 

For print and printf, dest-expr is an optional expression that directs the output to a file or pipe.


> file

Directs the output to a file, overwriting its previous contents.


>> file

Appends the output to a file, preserving its previous contents. In both of these cases, the file will be created if it does not already exist.


| command

Directs the output as the input to a system command.


|& command

Directs the output as the input to a coprocess. gawk only.

Be careful not to mix > and >> for the same file. Once a file has been opened with >, subsequent output statements continue to append to the file until it is closed.

Remember to call close( ) when you have finished with a file, pipe, or coprocess. If you don't, eventually you will hit the system limit on the number of simultaneously open files.

11.12.1. printf Formats

Format specifiers for printf and sprintf have the following form:

     %[posn$][flag][width][.precision]letter

The control letter is required. The format conversion control letters are given in the following table.

Character

Description

c

ASCII character.

d

Decimal integer.

i

Decimal integer. (Added in POSIX)

e

Floating-point format ([-]d.precisione[+-]dd).

E

Floating-point format ([-]d.precisionE[+-]dd).

f

Floating-point format ([-]ddd.precision).

g

e or f conversion, whichever is shortest, with trailing zeros removed.

G

E or f conversion, whichever is shortest, with trailing zeros removed.

o

Unsigned octal value.

s

String.

u

Unsigned decimal value.

x

Unsigned hexadecimal number. Uses a-f for 10 to 15.

X

Unsigned hexadecimal number. Uses A-F for 10 to 15.

%

Literal %.


gawk allows you to provide a positional specifier after the % (posn$). A positional specifier is an integer count followed by a $. The count indicates which argument to use at that point. Counts start at one, and don't include the format string. This feature is primarily for use in producing translations of format strings. For example:

     $ gawk 'BEGIN { printf "%2$s, %1$s\n", "world", "hello" }'     hello, world

The optional flag is one of the following:

Character

Description

-

Left-justify the formatted value within the field.

space

Prefix positive values with a space and negative values with a minus.

+

Always prefix numeric values with a sign, even if the value is positive.

#

Use an alternate form: %o has a preceding 0; %x and %X are prefixed with 0x and 0X, respectively; %e, %E, and %f always have a decimal point in the result; and %g and %G do not have trailing zeros removed.

0

Pad output with zeros, not spaces. This only happens when the field width is wider than the converted result. This flag applies to all output formats, even nonnumeric ones. (Unfortunately, not all awk implementations do this correctly.)

'

gawk 3.1.4 and later only. For numeric formats, in locales that support it, supply a thousands-separator character.


The optional width is the minimum number of characters to output. The result will be padded to this size if it is smaller. The 0 flag causes padding with zeros; otherwise, padding is with spaces.

The precision is optional. Its meaning varies by control letter, as shown in this table:

Conversion

Precision means

%d, %i, %o, %u, %x, %X

The minimum number of digits to print.

%e, %E, %f

The number of digits to the right of the decimal point.

%g, %G

The maximum number of significant digits.

%s

The maximum number of characters to print.


     < Day Day Up > 


    Unix in a Nutshell
    Unix in a Nutshell, Fourth Edition
    ISBN: 0596100299
    EAN: 2147483647
    Year: 2005
    Pages: 201

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net