手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux awk 内置变量实例

[日期:2013-01-09] 来源:Linux社区  作者:sunboy_2050 [字体:大 中 小]

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.
linux
相关资讯       awk  awk 内置变量  awk 变量 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款