Go forward to Printf Examples.
Go backward to Control Letters.
Go up to Printf.

Modifiers for `printf' Formats
------------------------------

   A format specification can also include "modifiers" that can control
how much of the item's value is printed and how much space it gets.  The
modifiers come between the `%' and the format-control letter.  Here are
the possible modifiers, in the order in which they may appear:

`-'
     The minus sign, used before the width modifier, says to
     left-justify the argument within its specified width.  Normally
     the argument is printed right-justified in the specified width.
     Thus,

          printf "%-4s", "foo"

     prints `foo '.

`WIDTH'
     This is a number representing the desired width of a field.
     Inserting any number between the `%' sign and the format control
     character forces the field to be expanded to this width.  The
     default way to do this is to pad with spaces on the left.  For
     example,

          printf "%4s", "foo"

     prints ` foo'.

     The value of WIDTH is a minimum width, not a maximum.  If the item
     value requires more than WIDTH characters, it can be as wide as
     necessary.  Thus,

          printf "%4s", "foobar"

     prints `foobar'.

     Preceding the WIDTH with a minus sign causes the output to be
     padded with spaces on the right, instead of on the left.

`.PREC'
     This is a number that specifies the precision to use when printing.
     This specifies the number of digits you want printed to the right
     of the decimal point.  For a string, it specifies the maximum
     number of characters from the string that should be printed.

   The C library `printf''s dynamic WIDTH and PREC capability (for
example, `"%*.*s"') is supported.  Instead of supplying explicit WIDTH
and/or PREC values in the format string, you pass them in the argument
list.  For example:

     w = 5
     p = 3
     s = "abcdefg"
     printf "<%*.*s>\n", w, p, s

is exactly equivalent to

     s = "abcdefg"
     printf "<%5.3s>\n", s

Both programs output `<**abc>'.  (We have used the bullet symbol "*" to
represent a space, to clearly show you that there are two spaces in the
output.)

   Earlier versions of `awk' did not support this capability.  You may
simulate it by using concatenation to build up the format string, like
so:

     w = 5
     p = 3
     s = "abcdefg"
     printf "<%" w "." p "s>\n", s

This is not particularly easy to read, however.