Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / 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
ENVIRONUNIX环境变量
ERRNOUNIX系统错误消息
FIELDWIDTHS输入字段宽度的空白分隔字符串
FNR当前记录数
OFMT数字的输出格式 %.6g
RSTART被匹配函数匹配的字符串首
RLENGTH被匹配函数匹配的字符串长度
SUBSEP34
 

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.