User's Guide for Awk
Formatting with printf
echo 123.4567 | awk '{printf "%.3f\n", $1}'
123.457
# 不满8字符向左填充空格,并且只保留一位小数
echo 123.4567 | awk '{printf "%8.1f\n", $1}'
123.5
# 第一个元素不满20字符向右填充空格,并且保留七位小数;第二个元素以整形输出
echo 123.4567 55.2 | awk '{printf "%-20.7f %d\n" , $1 , $2}'
123.4567000 55`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.BEGIN/END
Built-in Variables
FS Example: 输入字段分隔符变量
OFS Example: 输出字段分隔符变量
RS Example: 输入的记录分隔符变量
ORS Example: 输出的记录分隔符变量
NR Example: 记录数变量
NF Example: 当前记录中的字段数变量
FILENAME Example: 当前输入的文件名
FNR Example: 相对于当前输入文件的记录数
Expressions as Patterns
Comparison expression
Pattern match
Example
See also
Last updated