########################################## -->>> List of the format-control letters: `c' This prints a number as an ASCII character. Thus, `printf "%c", 65' outputs the letter `A'. The output for a string value is the first character of the string. `d' This prints a decimal integer. `i' This also prints a decimal integer. `e' This prints a number in scientific (exponential) notation. For example, printf "%4.3e", 1950 prints `1.950e+03', with a total of four significant figures of which three follow the decimal point. The `4.3' are modifiers, discussed below. `f' This prints a number in floating point notation. `g' This prints a number in either scientific notation or floating point notation, whichever uses fewer characters. `o' This prints an unsigned octal integer. `s' This prints a string. `x' This prints an unsigned hexadecimal integer. `X' This prints an unsigned hexadecimal integer. However, for the values 10 through 15, it uses the letters `A' through `F' instead of `a' through `f'. `%' This isn't really a format-control letter, but it does have a meaning when used after a `%': the sequence `%%' outputs one `%'. It does not consume an argument. ########################################## --->>> Modifiers for printf Formatting 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. printf "%.3f\n", 123.4567 prints `123.457 3 (rounded) digits after the decimal ########################################## --->>> Padding Zeros echo 3 | awk '{printf "%05f\n", $1}' 3.000000 echo $i | awk '{printf "%05.f\n", $1}' 00003 echo 3 | awk '{printf "%05s\n", $1}' 3 echo $i | awk '{printf "%05d\n", $1}' 00003 ########################################## --->>> Floating Point Examples echo 123.4567 | awk '{printf "%.3f\n", $1}' 123.457 echo 123.4567 | awk '{printf "%.1f\n", $1}' 123.5 echo 123.4567 | awk '{printf "%2.1f\n", $1}' 123.5 echo 123.4567 | awk '{printf "%5.1f\n", $1}' 123.5 echo 123.4567 | awk '{printf "%8.1f\n", $1}' 123.5 echo 123.4567 | awk '{printf "%8.6f\n", $1}' 123.456700 echo 123.4567 | awk '{printf "%.2e\n", $1}' 1.23e+02 echo 123.4567 | awk '{printf "%.4e\n", $1}' 1.2346e+02 echo 123.4567 55.2 | awk '{printf "%.3f", $1; print $2}' 123.45755.2 echo 123.4567 55.2 | awk '{printf "%.3f ", $1; print $2}' 123.457 55.2 echo 123.4567 55.2 | awk '{printf "%-20.7f %d\n" , $1 , $2}' 123.4567000 55 echo 123.4567 55.2 | awk '{printf "%20.7f %d\n" , $1 , $2} 123.4567000 55 foreach i = (`seq 1 3`) echo $i | awk '{printf "%05s", $1}'