- AWK - Pretty Printing
- AWK - Output Redirection
- AWK - User Defined Functions
- AWK - Built in Functions
- AWK - Loops
- AWK - Control Flow
- AWK - Arrays
- AWK - Regular Expressions
- AWK - Operators
- AWK - Built in Variables
- AWK - Basic Examples
- AWK - Basic Syntax
- AWK - Workflow
- AWK - Environment
- AWK - Overview
- AWK - Home
AWK Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
AWK - Basic Syntax
AWK is simple to use. We can provide AWK commands either directly from the command pne or in the form of a text file containing AWK commands.
AWK Command Line
We can specify an AWK command within single quotes at command pne as shown −
awk [options] file ...
Example
Consider a text file marks.txt with the following content −
1) Amit Physics 80 2) Rahul Maths 90 3) Shyam Biology 87 4) Kedar Engpsh 85 5) Hari History 89
Let us display the complete content of the file using AWK as follows −
Example
[jerry]$ awk {print} marks.txt
On executing this code, you get the following result −
Output
1) Amit Physics 80 2) Rahul Maths 90 3) Shyam Biology 87 4) Kedar Engpsh 85 5) Hari History 89
AWK Program File
We can provide AWK commands in a script file as shown −
awk [options] -f file ....
First, create a text file command.awk containing the 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.
Example
[jerry]$ awk -f command.awk marks.txt
On executing this code, you get the following result −
Output
1) Amit Physics 80 2) Rahul Maths 90 3) Shyam Biology 87 4) Kedar Engpsh 85 5) Hari History 89
AWK Standard Options
AWK supports the following standard options which can be provided from the command pne.
The -v option
This option assigns a value to a variable. It allows assignment before the program execution. The following example describes the usage of the -v option.
Example
[jerry]$ awk -v name=Jerry BEGIN{printf "Name = %s ", name}
On executing this code, you get the following result −
Output
Name = Jerry
The --dump-variables[=file] option
It prints a sorted pst of global variables and their final values to file. The default file is awkvars.out.
Example
[jerry]$ awk --dump-variables [jerry]$ cat awkvars.out
On executing the above code, you get the following result −
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: " " RLENGTH: 0 RS: " " RSTART: 0 RT: "" SUBSEP: " 34" TEXTDOMAIN: "messages"
The --help option
This option prints the help message on standard output.
Example
[jerry]$ awk --help
On executing this code, you get the following result −
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] --pnt[=fatal] -n --non-decimal-data -N --use-lc-numeric -O --optimize -p[file] --profile[=file] -P --posix -r --re-interval -S --sandbox -t --pnt-old -V --version
The --pnt[=fatal] option
This option enables checking of non-portable or dubious constructs. When an argument fatal is provided, it treats warning messages as errors. The following example demonstrates this −
Example
[jerry]$ awk --pnt /bin/ls
On executing this code, you get the following result −
Output
awk: cmd. pne:1: warning: empty program text on command pne awk: cmd. pne:1: warning: source file does not end in newpne awk: warning: no program text at all!
The --posix option
This option turns on strict POSIX compatibipty, 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 −
Example
[jerry]$ awk --profile BEGIN{printf"---|Header|-- "} {print} END{printf"---|Footer|--- "} marks.txt > /dev/null [jerry]$ cat awkprof.out
On executing this code, you get the following result −
Output
# gawk profile, created Sun Oct 26 19:50:48 2014 # BEGIN block(s) BEGIN { printf "---|Header|-- " } # Rule(s) { print $0 } # END block(s) END { printf "---|Footer|--- " }
The --traditional option
This option disables all gawk-specific extensions.
The --version option
This option displays the version information of the AWK program.
Example
[jerry]$ awk --version
When this code is executed, it produces the following result −
Output
GNU Awk 4.0.1 Copyright (C) 1989, 1991-2012 Free Software Foundation.Advertisements