21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 4: Printing Output 63%% This isn’t a format-control letter, but it does have meaning—the sequence ‘%%’outputs one ‘%’; it does not consume an argument and it ignores any modifiers.NOTE: When using the integer format-control letters for values that are outsidethe range of the widest C integer type, gawk switches to the ‘%g’ format specifier.If ‘--lint’ is provided on the command line (see Section 11.2 [Command-LineOptions], page 177), gawk warns about this. Other versions of awk may printinvalid values or do something else entirely.4.5.3 Modifiers for printf FormatsA format specification can also include modifiers that can control how much of the item’svalue is printed, as well as how much space it gets. The modifiers come between the ‘%’ andthe format-control letter. We will use the bullet symbol “•” in the following examples torepresent spaces in the output. Here are the possible modifiers, in the order in which theymay appear:N$ An integer constant followed by a ‘$’ is a positional specifier. Normally, formatspecifications are applied to arguments in the order given in the format string.With a positional specifier, the format specification is applied to a specificargument, instead of what would be the next argument in the list. Positionalspecifiers begin counting with one. Thus:printf "%s %s\n", "don’t", "panic"printf "%2$s %1$s\n", "panic", "don’t"prints the famous friendly message twice.At first glance, this feature doesn’t seem to be of much use. It is in fact a gawkextension, intended for use in translating messages at runtime. See Section 9.4.2[Rearranging printf Arguments], page 164, which describes how and why touse positional specifiers. For now, we will not use them.- The minus sign, used before the width modifier (see later on in this table), saysto left-justify the argument within its specified width. Normally, the argumentis printed right-justified in the specified width. Thus:printf "%-4s", "foo"prints ‘foo•’.spaceFor numeric conversions, prefix positive values with a space and negative valueswith a minus sign.+ The plus sign, used before the width modifier (see later on in this table), saysto always supply a sign for numeric conversions, even if the data to format ispositive. The ‘+’ overrides the space modifier.# Use an “alternate form” for certain control letters. For ‘%o’, supply a leadingzero. For ‘%x’ and ‘%X’, supply a leading ‘0x’ or ‘0X’ for a nonzero result. For‘%e’, ‘%E’, and ‘%f’, the result always contains a decimal point. For ‘%g’ and‘%G’, trailing zeros are not removed from the result.0 A leading ‘0’ (zero) acts as a flag that indicates that output should be paddedwith zeros instead of spaces. This applies only to the numeric output formats.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!