Awk 入门/Awk 快速参考指南
外观
< Awk 入门
本节提供了 Awk 编程的方便查找参考。
- 调用 Awk
awk [-F<ch>] {pgm} | {-f <pgm file>} [<vars>] [-|<data file>]
—在哪里
ch: Field-separator character. pgm: Awk command-line program. pgm file: File containing an Awk program. vars: Awk variable initializations. data file: Input data file.
- Awk 程序的一般形式
BEGIN {<initializations>} <search pattern 1> {<program actions>} <search pattern 2> {<program actions>} ... END {<final actions>}
- 搜索模式
/<string>/ Search for string. /^<string>/ Search for string at beginning of line. /<string>$/ Search for string at end of line.
搜索可以限制在特定字段
$<field> ~ /<string>/ Search for string in specified field. $<field> !~ /<string>/ Search for string \Inot\i in specified field.
字符串可以在搜索中进行 OR 操作
/(<string1>)|(<string2>)/
搜索可以针对由两个字符串界定的整个行范围
/<string1>/,/<string2>/
搜索可以针对任何条件,例如行号,可以使用以下比较运算符
== != < > <= >=
不同的条件可以使用 "||" 进行 OR 操作或使用 "&&" 进行 AND 操作。
[<charlist or range>] Match on any character in list or range. [^<charlist or range>] Match on any character not in list or range. . Match any single character. * Match 0 or more occurrences of preceding string. ? Match 0 or 1 occurrences of preceding string. + Match 1 or more occurrences of preceding string.
如果元字符是搜索字符串的一部分,则可以通过在前面添加 "\" 来 "转义" 它。
- 特殊字符
\n Newline (line feed).
Backspace. \r Carriage return. \f Form feed. A "\" can be embedded in a string by entering it twice: "\\".
- 内置变量
$0; $1,$2,$3,... Field variables. NR Number of records (lines). NF Number of fields. FILENAME Current input filename. FS Field separator character (default: " "). RS Record separator character (default: "\n"). OFS Output field separator (default: " "). ORS Output record separator (default: "\n"). OFMT Output format (default: "%.6g").
- 算术运算
+ Addition. - Subtraction. * Multiplication. / Division. % Mod. ++ Increment. -- Decrement.
简写赋值
x += 2 -- is the same as: x = x + 2 x -= 2 -- is the same as: x = x - 2 x *= 2 -- is the same as: x = x * 2 x /= 2 -- is the same as: x = x / 2 x %= 2 -- is the same as: x = x % 2
- 唯一的字符串操作是连接,只需列出由空格连接的两个字符串即可。
- 算术函数
sqrt() Square root. log() Base \Ie\i log. exp() Power of \Ie\i. int() Integer part of argument.
- 字符串函数
- length()
Length of string.
- substr(<string>,<start of substring>,<max length of substring>)
Get substring.
- split(<string>,<array>,[<field separator>])
Split string into array, with initial array index being 1.
- index(<target string>,<search string>)
Find index of search string in target string.
- sprintf()
Perform formatted print into string.
- 控制结构
if (<condition>) <action 1> [else <action 2>] while (<condition>) <action> for (<initial action>;<condition>;<end-of-loop action>) <action>
使用 "for" 扫描关联数组
for (<variable> in <array>) <action>
无条件控制语句
break Break out of "while" or "for" loop. continue Perform next iteration of "while" or "for" loop. next Get and scan next line of input. exit Finish reading input and perform END statements.
- 打印
print <i1>, <i2>, ... Print items separated by OFS; end with newline. print <i1> <i2> ... Print items concatenated; end with newline.
- Printf()
通用格式
printf(<string with format codes>,[<parameters>])
必须使用 "\n" 显式指定换行符。
格式代码的一般形式
%[<number>]<format code>
可选的 "number" 可以包括
- 用于左对齐输出的开头 "-"
- 指定最小输出宽度的整数部分。 (开头 "0"
causes the output to be padded with zeroes.)
- 指定要打印的最大字符数(对于字符串)或要打印的小数点右侧的位数(对于浮点格式)的小数部分。
格式代码为
d Prints a number in decimal format. o Prints a number in octal format. x Prints a number in hexadecimal format. c Prints a character, given its numeric code. s Prints a string. e Prints a number in exponential format. f Prints a number in floating-point format. g Prints a number in exponential or floating-point format.
- Awk 可以从 "print" 和 "printf" 中执行输出重定向(使用 ">" 和 ">>")和管道(使用 "|")。