gawk: Format Modifiers

1 
1 5.5.3 Modifiers for 'printf' Formats
1 ------------------------------------
1 
1 A format specification can also include "modifiers" that can control how
1 much of the item's value is printed, as well as how much space it gets.
1 The modifiers come between the '%' and the format-control letter.  We
1 use the bullet symbol "*" in the following examples to represent spaces
1 in the output.  Here are the possible modifiers, in the order in which
1 they may appear:
1 
1 'N$'
1      An integer constant followed by a '$' is a "positional specifier".
1      Normally, format specifications are applied to arguments in the
1      order given in the format string.  With a positional specifier, the
1      format specification is applied to a specific argument, instead of
1      what would be the next argument in the list.  Positional specifiers
1      begin counting with one.  Thus:
1 
1           printf "%s %s\n", "don't", "panic"
1           printf "%2$s %1$s\n", "panic", "don't"
1 
1      prints the famous friendly message twice.
1 
1      At first glance, this feature doesn't seem to be of much use.  It
1      is in fact a 'gawk' extension, intended for use in translating
1      messages at runtime.  ⇒Printf Ordering, which describes how
1      and why to use positional specifiers.  For now, we ignore them.
1 
1 '-' (Minus)
1      The minus sign, used before the width modifier (see later on in
1      this list), says to left-justify the argument within its specified
1      width.  Normally, the argument is printed right-justified in the
1      specified width.  Thus:
1 
1           printf "%-4s", "foo"
1 
1      prints 'foo*'.
1 
1 SPACE
1      For numeric conversions, prefix positive values with a space and
1      negative values with a minus sign.
1 
1 '+'
1      The plus sign, used before the width modifier (see later on in this
1      list), says to always supply a sign for numeric conversions, even
1      if the data to format is positive.  The '+' overrides the space
1      modifier.
1 
1 '#'
1      Use an "alternative form" for certain control letters.  For '%o',
1      supply a leading zero.  For '%x' and '%X', supply a leading '0x' or
1      '0X' for a nonzero result.  For '%e', '%E', '%f', and '%F', the
1      result always contains a decimal point.  For '%g' and '%G',
1      trailing zeros are not removed from the result.
1 
1 '0'
1      A leading '0' (zero) acts as a flag indicating that output should
1      be padded with zeros instead of spaces.  This applies only to the
1      numeric output formats.  This flag only has an effect when the
1      field width is wider than the value to print.
1 
1 '''
1      A single quote or apostrophe character is a POSIX extension to ISO
1      C. It indicates that the integer part of a floating-point value, or
1      the entire part of an integer decimal value, should have a
1      thousands-separator character in it.  This only works in locales
1      that support such characters.  For example:
1 
1           $ cat thousands.awk          Show source program
1           -| BEGIN { printf "%'d\n", 1234567 }
1           $ LC_ALL=C gawk -f thousands.awk
1           -| 1234567                   Results in "C" locale
1           $ LC_ALL=en_US.UTF-8 gawk -f thousands.awk
1           -| 1,234,567                 Results in US English UTF locale
1 
1      For more information about locales and internationalization issues,
1      see ⇒Locales.
1 
1           NOTE: The ''' flag is a nice feature, but its use complicates
1           things: it becomes difficult to use it in command-line
1           programs.  For information on appropriate quoting tricks, see
1           ⇒Quoting.
1 
1 WIDTH
1      This is a number specifying the desired minimum width of a field.
1      Inserting any number between the '%' sign and the format-control
1      character forces the field to expand to this width.  The default
1      way to do this is to pad with spaces on the left.  For example:
1 
1           printf "%4s", "foo"
1 
1      prints '*foo'.
1 
1      The value of WIDTH is a minimum width, not a maximum.  If the item
1      value requires more than WIDTH characters, it can be as wide as
1      necessary.  Thus, the following:
1 
1           printf "%4s", "foobar"
1 
1      prints 'foobar'.
1 
1      Preceding the WIDTH with a minus sign causes the output to be
1      padded with spaces on the right, instead of on the left.
1 
1 '.PREC'
1      A period followed by an integer constant specifies the precision to
1      use when printing.  The meaning of the precision varies by control
1      letter:
1 
1      '%d', '%i', '%o', '%u', '%x', '%X'
1           Minimum number of digits to print.
1 
1      '%e', '%E', '%f', '%F'
1           Number of digits to the right of the decimal point.
1 
1      '%g', '%G'
1           Maximum number of significant digits.
1 
1      '%s'
1           Maximum number of characters from the string that should
1           print.
1 
1      Thus, the following:
1 
1           printf "%.4s", "foobar"
1 
1      prints 'foob'.
1 
1    The C library 'printf''s dynamic WIDTH and PREC capability (e.g.,
1 '"%*.*s"') is supported.  Instead of supplying explicit WIDTH and/or
1 PREC values in the format string, they are passed in the argument list.
1 For example:
1 
1      w = 5
1      p = 3
1      s = "abcdefg"
1      printf "%*.*s\n", w, p, s
1 
1 is exactly equivalent to:
1 
1      s = "abcdefg"
1      printf "%5.3s\n", s
1 
1 Both programs output '**abc'.  Earlier versions of 'awk' did not support
1 this capability.  If you must use such a version, you may simulate this
1 feature by using concatenation to build up the format string, like so:
1 
1      w = 5
1      p = 3
1      s = "abcdefg"
1      printf "%" w "." p "s\n", s
1 
1 This is not particularly easy to read, but it does work.
1 
1    C programmers may be used to supplying additional modifiers ('h',
1 'j', 'l', 'L', 't', and 'z') in 'printf' format strings.  These are not
1 valid in 'awk'.  Most 'awk' implementations silently ignore them.  If
1 '--lint' is provided on the command line (⇒Options), 'gawk' warns
1 about their use.  If '--posix' is supplied, their use is a fatal error.
1