AWK - 基本語法



AWK 使用簡單。我們可以直接在命令列中提供 AWK 命令,也可以以包含 AWK 命令的文字檔案形式提供。

AWK 命令列

我們可以在命令列中使用單引號指定 AWK 命令,如下所示:

awk [options] file ...

示例

考慮一個文字檔案marks.txt,其內容如下:

1) Amit     Physics    80
2) Rahul    Maths      90
3) Shyam    Biology    87
4) Kedar    English    85
5) Hari     History    89

讓我們使用 AWK 顯示檔案的完整內容,如下所示:

示例

[jerry]$ awk '{print}' marks.txt 

執行此程式碼後,您將獲得以下結果:

輸出

1) Amit     Physics    80
2) Rahul    Maths      90
3) Shyam    Biology    87
4) Kedar    English    85
5) Hari     History    89

AWK 程式檔案

我們可以將 AWK 命令放在指令碼檔案中,如下所示:

awk [options] -f file ....

首先,建立一個名為command.awk的文字檔案,其中包含如下所示的 AWK 命令:

{print}

現在我們可以指示 AWK 從文字檔案讀取命令並執行操作。在這裡,我們實現了與上面示例中相同的結果。

示例

[jerry]$ awk -f command.awk marks.txt

執行此程式碼後,您將獲得以下結果:

輸出

1) Amit  Physics 80
2) Rahul Maths   90
3) Shyam Biology 87
4) Kedar English 85
5) Hari  History 89

AWK 標準選項

AWK 支援以下標準選項,這些選項可以從命令列提供。

-v 選項

此選項將值分配給變數。它允許在程式執行之前進行賦值。以下示例描述了 -v 選項的用法。

示例

[jerry]$ awk -v name=Jerry 'BEGIN{printf "Name = %s\n", name}'

執行此程式碼後,您將獲得以下結果:

輸出

Name = Jerry

--dump-variables[=file] 選項

它將全域性變數及其最終值的排序列表列印到檔案中。預設檔案為awkvars.out

示例

[jerry]$ awk --dump-variables ''
[jerry]$ cat awkvars.out 

執行上述程式碼後,您將獲得以下結果:

輸出

ARGC: 1
ARGIND: 0
ARGV: array, 1 elements
BINMODE: 0
CONVFMT: "%.6g"
ERRNO: ""
FIELDWIDTHS: ""
FILENAME: ""
FNR: 0
FPAT: "[^[:space:]]+"
FS: " "
IGNORECASE: 0
LINT: 0
NF: 0
NR: 0
OFMT: "%.6g"
OFS: " "
ORS: "\n"
RLENGTH: 0
RS: "\n"
RSTART: 0
RT: ""
SUBSEP: "\034"
TEXTDOMAIN: "messages"

--help 選項

此選項在標準輸出上列印幫助訊息。

示例

[jerry]$ awk --help

執行此程式碼後,您將獲得以下結果:

輸出

Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options : GNU long options: (standard)
   -f progfile                --file=progfile
   -F fs                      --field-separator=fs
   -v var=val                 --assign=var=val
Short options : GNU long options: (extensions)
   -b                         --characters-as-bytes
   -c                         --traditional
   -C                         --copyright
   -d[file]                   --dump-variables[=file]
   -e 'program-text'          --source='program-text'
   -E file                    --exec=file
   -g                         --gen-pot
   -h                         --help
   -L [fatal]                 --lint[=fatal]
   -n                         --non-decimal-data
   -N                         --use-lc-numeric
   -O                         --optimize
   -p[file]                   --profile[=file]
   -P                         --posix
   -r                         --re-interval
   -S                         --sandbox
   -t                         --lint-old
   -V                         --version

--lint[=fatal] 選項

此選項啟用對不可移植或可疑結構的檢查。當提供引數fatal時,它將警告訊息視為錯誤。以下示例演示了這一點:

示例

[jerry]$ awk --lint '' /bin/ls

執行此程式碼後,您將獲得以下結果:

輸出

awk: cmd. line:1: warning: empty program text on command line
awk: cmd. line:1: warning: source file does not end in newline
awk: warning: no program text at all!

--posix 選項

此選項開啟嚴格的 POSIX 相容性,其中所有常用和 gawk 特定的擴充套件都被停用。

--profile[=file] 選項

此選項在檔案中生成程式的漂亮列印版本。預設檔案為awkprof.out。以下簡單示例說明了這一點:

示例

[jerry]$ awk --profile 'BEGIN{printf"---|Header|--\n"} {print} 
END{printf"---|Footer|---\n"}' marks.txt > /dev/null 
[jerry]$ cat awkprof.out

執行此程式碼後,您將獲得以下結果:

輸出

# gawk profile, created Sun Oct 26 19:50:48 2014

   # BEGIN block(s)

   BEGIN {
      printf "---|Header|--\n"
   }

   # Rule(s) {
      print $0
   }

   # END block(s)

   END {
      printf "---|Footer|---\n"
   }

--traditional 選項

此選項停用所有 gawk 特定的擴充套件。

--version 選項

此選項顯示 AWK 程式的版本資訊。

示例

[jerry]$ awk --version

當執行此程式碼時,它會產生以下結果:

輸出

GNU Awk 4.0.1
Copyright (C) 1989, 1991-2012 Free Software Foundation.
廣告

© . All rights reserved.