awk

w

 

https://zh.wikipedia.org/wiki/Awk

https://en.wikipedia.org/wiki/AWK

"AWK is a language for processing text files. A file is treated as a sequence of records, and by default each line is a record. Each line is broken up into a sequence of fields, so we can think of the first word in a line as the first field, the second word as the second field, and so on. An AWK program is a sequence of pattern-action statements. AWK reads the input a line at a time. A line is scanned for each pattern in the program, and for each pattern that matches, the associated action is executed." - Alfred V. Aho[8]

 

 

Built-in variables[edit]

Awk's built-in variables include the field variables: $1, $2, $3, and so on ($0 represents the entire record). They hold the text or values in the individual text-fields in a record.

Other variables include:

  • NR: Keeps a current count of the number of input records.
  • NF: Keeps a count of the number of fields in an input record. The last field in the input record can be designated by $NF.
  • FILENAME: Contains the name of the current input-file.
  • FS: Contains the "field separator" character used to divide fields on the input record. The default, "white space", includes any space and tab characters. FS can be reassigned to another character to change the field separator.
  • RS: Stores the current "record separator" character. Since, by default, an input line is the input record, the default record separator character is a "newline".
  • OFS: Stores the "output field separator", which separates the fields when Awk prints them. The default is a "space" character.
  • ORS: Stores the "output record separator", which separates the output records when Awk prints them. The default is a "newline" character.
  • OFMT: Stores the format for numeric output. The default format is "%.6g".

 

内建变量[编辑]

AWK的内建变量包括域变量,例如$1, $2, $3,以及$0。这些变量给出了记录中域的内容。 内建变量也包括一些其他变量:

  • NR:已输入记录的条数。
  • NF:当前记录中域的个数。记录中最后一个域可以以$NF的方式引用。
  • FILENAME:当前输入文件的文件名。
  • FS:“域分隔符”,用于将输入记录分割成域。其默认值为“空白字符”,即空格和制表符。FS可以替换为其它字符,从而改变域分隔符。
  • RS:当前的“记录分隔符”。默认状态下,输入的每行都被作为一个记录,因此默认记录分隔符是换行符
  • OFS:“输出域分隔符”,即分隔print命令的参数的符号。其默认值为空格。
  • ORS:“输出记录分隔符”,即每个print命令之间的符号。其默认值为换行符。
  • OFMT:“输出数字格式”(Format for numeric output),其默认值为"%.6g"。

 

posted @ 2017-04-14 17:57  papering  阅读(188)  评论(0编辑  收藏  举报