AWK is simple to use and we can provide AWK commands either directly
from command line or in the form of a text file having AWK commands.
This tutorial explain these two ways of invoking AWK with suitable
examples:
AWK Command Line
Following is the form where we can specify AWK command within single quotes at command line itself:awk [options] file ...
Example
Consider we have a text file marks.txt to be processed and it has following content:1) Amit Physics 80 2) Rahul Maths 90 3) Shyam Biology 87 4) Kedar English 85 5) Hari History 89Let's display complete content of the file using AWK as follows:
[jerry]$ awk '{print}' marks.txt
On executing the above code, you get the following result:1) Amit Physics 80 2) Rahul Maths 90 3) Shyam Biology 87 4) Kedar English 85 5) Hari History 89
AWK Program File
Following is the second form where we can provide AWK commands in a script file:awk [options] -f file ....First, create a text command.awk file containing AWK command as shown below:
{print}
Now we can instruct the AWK to read commands from the text file and
perform the action. Here, we achieve the same result as shown in the
above example.[jerry]$ awk -f command.awk marks.txtOn executing the above code, you get the following result:
1) Amit Physics 80 2) Rahul Maths 90 3) Shyam Biology 87 4) Kedar English 85 5) Hari History 89
AWK Standard Options
AWK supports the following standard options which can be provided from command line.The -v option
This option assigns a value to variable. It allows assignment before the program execution. Below simple example describes the usage of the -v option.[jerry]$ awk -v name=Jerry 'BEGIN{printf "Name = %s\n", name}'
On executing the above code, you get the following result:Name = Jerry
The --dump-variables[=file] option
It prints a sorted list of global variables and their final values to file. The default file is awkvars.out.[jerry]$ awk --dump-variables '' [jerry]$ cat awkvars.outOn executing the above code, you get the following result:
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"
The --help option
This option prints the help message on standard output.[jerry]$ awk --helpOn executing the above code, you get the following result:
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
The --lint[=fatal] option
This option enables checking of non-portable or dubious constructs. When an argument fatal is provided, it treats warnings messages as errors. Below simple example illustrates this:[jerry]$ awk --lint '' /bin/lsOn executing the above code, you get the following result:
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!
The --posix option
This option turns on strict POSIX compatibility, in which all common and gawk-specific extensions are disabled.The --profile[=file] option
This option generates a pretty-printed version of the program in file. Default file is awkprof.out. Below simple example illustrates this:[jerry]$ awk --profile 'BEGIN{printf"---|Header|--\n"} {print} END{printf"---|Footer|---\n"}' marks.txt > /dev/null
[jerry]$ cat awkprof.out
On executing the above code, you get the following result:# 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"
}
The --traditional option
This option disables all gawk-specific extensions.The --version option
This option displays the version information of the AWK program.[jerry]$ awk --versionWhen above code is executed, it will produce following result:
GNU Awk 4.0.1 Copyright (C) 1989, 1991-2012 Free Software Foundation.

No comments:
Post a Comment