4.0 Fundamental Syntax and Basic Operations
4.1. Invoking AWK Programs
AWK programs can be executed in two primary ways: directly from the command line for simple, one-off tasks, or from a script file for more complex or reusable logic. This section covers both invocation methods and introduces foundational operations like field selection and pattern matching.
4.2. Invocation Methods
Command-Line Invocation
This method is best suited for short scripts. The entire program is provided as a string enclosed in single quotes.
- Syntax: awk [options] ‘program’ file …
For example, to print the entire content of marks.txt, we can use the print action:
[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
Script File Invocation
For longer or reusable scripts, it is better to place the AWK commands in a file. The -f option tells AWK to read the program from this file.
- Syntax: awk [options] -f file …
First, create a file named command.awk with the following content:
{print}
Now, execute it with the -f flag:
[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
4.3. Core Operations with Examples
Using the marks.txt file, let’s explore some of AWK’s most common operations.
Printing Specific Fields (Columns)
AWK automatically splits each record into fields based on whitespace. These fields are accessed using the syntax $n, where $1 is the first field, $2 is the second, and so on. The following command prints only the third and fourth columns, separated by a tab.
[jerry]$ awk ‘{print $3 “\t” $4}’ marks.txt
Output:
Physics 80
Maths 90
Biology 87
English 85
History 89
Pattern Matching and Record Printing
AWK can filter records, printing only those that match a specified pattern. $0 is a special variable that represents the entire record. The following command prints all lines containing the letter a.
[jerry]$ awk ‘/a/ {print $0}’ marks.txt
When no action block is provided, AWK’s default action is to print the matching line ({print $0}). Therefore, the command can be abbreviated:
[jerry]$ awk ‘/a/’ marks.txt
Output:
2) Rahul Maths 90
3) Shyam Biology 87
4) Kedar English 85
5) Hari History 89
Combining Pattern Matching and Field Selection
You can combine pattern matching with field selection to print specific columns only from the lines that match the pattern.
[jerry]$ awk ‘/a/ {print $3 “\t” $4}’ marks.txt
Output:
Maths 90
Biology 87
English 85
History 89
Performing Simple Calculations
AWK can perform actions based on patterns. The following script counts the number of lines containing the letter a. The counter cnt is incremented in the body for each match, and the final result is printed in the END block.
[jerry]$ awk ‘/a/{++cnt} END {print “Count = “, cnt}’ marks.txt
Output:
Count = 4
Using Built-in Functions for Conditions
Built-in functions can be used to form conditional patterns. The length() function returns the length of a string. The following command prints only lines that are longer than 18 characters.
[jerry]$ awk ‘length($0) > 18’ marks.txt
Output:
3) Shyam Biology 87
4) Kedar English 85
These basic operations demonstrate AWK’s power. To unlock more complex logic, we must explore the built-in variables that AWK provides.