21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

64 <strong>G<strong>AWK</strong></strong>: <strong>Effective</strong> <strong>AWK</strong> <strong>Programming</strong>This flag only has an effect when the field width is wider than the value toprint.’ A single quote or apostrophe character is a POSIX extension to ISO C. Itindicates that the integer part of a floating point value, or the entire part of aninteger decimal value, should have a thousands-separator character in it. Thisonly works in locales that support such characters. For example:width.prec$ cat thousands.awk Show source program⊣ BEGIN { printf "%’d\n", 1234567 }$ LC_ALL=C gawk -f thousands.awk⊣ 1234567Results in "C" locale$ LC_ALL=en_US.UTF-8 gawk -f thousands.awk⊣ 1,234,567Results in US English UTF localeFor more information about locales and internationalization issues, seeSection 2.9 [Where You Are Makes A Difference], page 35.NOTE: The ‘’’ flag is a nice feature, but its use complicates things:it becomes difficult to use it in command-line programs. For informationon appropriate quoting tricks, see Section 1.1.6 [Shell-Quoting Issues], page 14.This is a number specifying the desired minimum width of a field. Insertingany number between the ‘%’ sign and the format-control character forces thefield to expand to this width. The default way to do this is to pad with spaceson the left. For example:printf "%4s", "foo"prints ‘•foo’.The value of width is a minimum width, not a maximum. If the item valuerequires more than width characters, it can be as wide as necessary. Thus, thefollowing:printf "%4s", "foobar"prints ‘foobar’.Preceding the width with a minus sign causes the output to be padded withspaces on the right, instead of on the left.A period followed by an integer constant specifies the precision to use whenprinting. The meaning of the precision varies by control letter:%e, %E, %f%g, %GNumber of digits to the right of the decimal point.Maximum number of significant digits.%d, %i, %o, %u, %x, %XMinimum number of digits to print.%s Maximum number of characters from the string that should print.Thus, the following:printf "%.4s", "foobar"prints ‘foob’.

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

Saved successfully!

Ooh no, something went wrong!