目录

AWK - Basic 语法

AWK易于使用。 我们可以直接从命令行或以包含AWK命令的文本文件的形式提供AWK命令。

AWK Command Line

我们可以在命令行的单引号内指定一个AWK命令,如图所示 -

awk [options] file ...

例子 (Example)

考虑一个带有以下内容的文本文件marks.txt -

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

让我们使用AWK显示文件的完整内容,如下所示 -

Example

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

执行此代码时,您将获得以下结果 -

Output

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 ....

首先,创建一个包含AWK命令的文本文件command.awk ,如下所示 -

{print}

现在我们可以指示AWK从文本文件中读取命令并执行操作。 在这里,我们获得了与上例中所示相同的结果。

Example

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

执行此代码时,您将获得以下结果 -

Output

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

AWK标准选项

AWK支持以下标准选项,可以从命令行提供。

-v选项

此选项为变量赋值。 它允许在程序执行之前进行分配。 以下示例描述了-v选项的用法。

Example

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

执行此代码时,您将获得以下结果 -

Output

Name = Jerry

--dump-variables [= file]选项

它将全局变量的排序列表及其最终值打印到文件中。 默认文件是awkvars.out

Example

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

执行上面的代码时,您会得到以下结果 -

Output

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选项

此选项在标准输出上打印帮助消息。

Example

[jerry]$ awk --help

执行此代码时,您将获得以下结果 -

Output

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参数时,它会将警告消息视为错误。 以下示例演示了这一点 -

Example

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

执行此代码时,您将获得以下结果 -

Output

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 。 以下简单示例说明了这一点

Example

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

执行此代码时,您将获得以下结果 -

Output

# 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"
   }

- 传统选项

此选项禁用所有特定于gawk的扩展。

--version选项

此选项显示AWK程序的版本信息。

Example

[jerry]$ awk --version

执行此代码时,会产生以下结果 -

Output

GNU Awk 4.0.1
Copyright (C) 1989, 1991-2012 Free Software Foundation.
↑回到顶部↑
WIKI教程 @2018