Linux awk 内置变量实例
Linux awk 内置变量实例
awk 是一门非常优秀的文本处理工具,甚至可以上升作为一门程序设计语言。
它处理文本的速度是快得惊人的,现在很多基于shell 日志分析工具都可以用它完成。特点是设计简单,速度表现很好,本文将介绍awk内置变量。
格式: awk [ -F re] [parameter...] ['pattern {action}' ] [-f progfile][in_file...]
一、内置变量
属 性 | 说 明 |
$0 | 当前记录行,代表一行记录 |
$1~$n | 当前记录的第n个字段,字段间由FS分隔 |
FS | 输入字段分隔符,默认是空格 |
NF | 当前记录中的字段个数,就是有多少列,一般取最后一列字段 |
NR | 已经读出的记录数,就是行号,从1开始 |
RS | 输入的记录分隔符,默认为换行符 |
OFS | 输出字段分隔符,默是空格 |
ORS | 输出的记录分隔符,默认为换行符 |
ARGC | 命令行参数个数 |
ARGV | 命令行参数数组 |
FILENAME | 当前输入文件的名字 |
IGNORECASE | 如果为真,则进行忽略大小写的匹配 |
ARGIND | 当前被处理文件的ARGV标志符 |
CONVFMT | 数字转换格式 %.6g |
ENVIRON | UNIX环境变量 |
ERRNO | UNIX系统错误消息 |
FIELDWIDTHS | 输入字段宽度的空白分隔字符串 |
FNR | 当前记录数 |
OFMT | 数字的输出格式 %.6g |
RSTART | 被匹配函数匹配的字符串首 |
RLENGTH | 被匹配函数匹配的字符串长度 |
SUBSEP | \034 |
Built-in variables
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.
- 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".
- FILENAME: Contains the name of the current input-file.
|
评论暂时关闭