歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> 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 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.
Copyright © Linux教程網 All Rights Reserved