跳轉到內容

Awk 入門/Awk 快速參考指南

來自華夏公益教科書,開放的書籍,開放的世界

本節提供了 Awk 程式設計的方便查詢參考。

  • 呼叫 Awk
   awk [-F<ch>] {pgm} | {-f <pgm file>} [<vars>] [-|<data file>]

—在哪裡

   ch:          Field-separator character.    pgm:         Awk command-line program.    pgm file:    File containing an Awk program.    vars:        Awk variable initializations.    data file:   Input data file.
  • Awk 程式的一般形式
   BEGIN              {<initializations>}     <search pattern 1> {<program actions>}     <search pattern 2> {<program actions>}     ...    END                {<final actions>}

  • 搜尋模式
   /<string>/     Search for string.    /^<string>/    Search for string at beginning of line.    /<string>$/    Search for string at end of line.

搜尋可以限制在特定欄位

   $<field> ~ /<string>/   Search for string in specified field.    $<field> !~ /<string>/  Search for string \Inot\i in specified field.

字串可以在搜尋中進行 OR 操作

   /(<string1>)|(<string2>)/

搜尋可以針對由兩個字串界定的整個行範圍

   /<string1>/,/<string2>/

搜尋可以針對任何條件,例如行號,可以使用以下比較運算子

   == != < > <= >=

不同的條件可以使用 "||" 進行 OR 操作或使用 "&&" 進行 AND 操作。

   [<charlist or range>]   Match on any character in list or range.    [^<charlist or range>]  Match on any character not in list or range.    .                       Match any single character.    *                       Match 0 or more occurrences of preceding string.    ?                       Match 0 or 1 occurrences of preceding string.    +                       Match 1 or more occurrences of preceding string.

如果元字元是搜尋字串的一部分,則可以透過在前面新增 "\" 來 "轉義" 它。

  • 特殊字元
   \n     Newline (line feed).    
       Backspace.    \r     Carriage return.    \f     Form feed. A "\" can be embedded in a string by entering it twice: "\\".
  • 內建變數
   $0; $1,$2,$3,...  Field variables.    NR                Number of records (lines).    NF                Number of fields.    FILENAME          Current input filename.    FS                Field separator character (default: " ").    RS                Record separator character (default: "\n").    OFS               Output field separator (default: " ").    ORS               Output record separator (default: "\n").    OFMT              Output format (default: "%.6g").
  • 算術運算
   +   Addition.    -   Subtraction.    *   Multiplication.    /   Division.    %   Mod.    ++  Increment.    --  Decrement.

簡寫賦值

   x += 2  -- is the same as:  x = x + 2    x -= 2  -- is the same as:  x = x - 2    x *= 2  -- is the same as:  x = x * 2    x /= 2  -- is the same as:  x = x / 2    x %= 2  -- is the same as:  x = x % 2
  • 唯一的字串操作是連線,只需列出由空格連線的兩個字串即可。
  • 算術函式
   sqrt()     Square root.    log()      Base \Ie\i log.    exp()      Power of \Ie\i.    int()      Integer part of argument.
  • 字串函式
  • length()
  Length of string.
  • substr(<string>,<start of substring>,<max length of substring>)
  Get substring.
  • split(<string>,<array>,[<field separator>])
  Split string into array, with initial array index being 1.
  • index(<target string>,<search string>)
  Find index of search string in target string.
  • sprintf()
  Perform formatted print into string.
  • 控制結構
   if (<condition>) <action 1> [else <action 2>]    while (<condition>) <action>

   for (<initial action>;<condition>;<end-of-loop action>) <action>

使用 "for" 掃描關聯陣列

   for (<variable> in <array>) <action>

無條件控制語句

   break       Break out of "while" or "for" loop.    continue    Perform next iteration of "while" or "for" loop.    next        Get and scan next line of input.    exit        Finish reading input and perform END statements.

  • 列印
   print <i1>, <i2>, ...   Print items separated by OFS; end with newline.    print <i1> <i2> ...     Print items concatenated; end with newline.
  • Printf()

通用格式

   printf(<string with format codes>,[<parameters>])

必須使用 "\n" 顯式指定換行符。

格式程式碼的一般形式

   %[<number>]<format code>

可選的 "number" 可以包括

  • 用於左對齊輸出的開頭 "-"
  • 指定最小輸出寬度的整數部分。 (開頭 "0"
  causes the output to be padded with zeroes.)
  • 指定要列印的最大字元數(對於字串)或要列印的小數點右側的位數(對於浮點格式)的小數部分。

格式程式碼為

   d    Prints a number in decimal format.    o    Prints a number in octal format.    x    Prints a number in hexadecimal format.    c    Prints a character, given its numeric code.    s    Prints a string.    e    Prints a number in exponential format.    f    Prints a number in floating-point format.    g    Prints a number in exponential or floating-point format.
  • Awk 可以從 "print" 和 "printf" 中執行輸出重定向(使用 ">" 和 ">>")和管道(使用 "|")。
華夏公益教科書