6.0 Expressions and Operators in AWK
6.1. The Building Blocks of Logic
Like any complete programming language, AWK provides a rich set of operators for performing arithmetic, string manipulation, comparisons, and logical evaluations. These operators are the tools used to construct expressions, which form the core logic of any non-trivial AWK program.
6.2. Operator Categories
Arithmetic Operators
These operators perform standard mathematical calculations: + (addition), – (subtraction), * (multiplication), / (division), and % (modulus).
[jerry]$ awk ‘BEGIN { print “10 % 3 =”, 10 % 3 }’
# Output:
# 10 % 3 = 1
Increment and Decrement Operators
AWK supports pre- and post-increment (++i, i++) and pre- and post-decrement (–i, i–) operators to modify numeric values by one.
Assignment Operators
These operators assign values to variables. They include simple assignment (=) and compound assignments such as +=, -=, *=, /=, and %=, which perform an operation and an assignment in a single step.
[jerry]$ awk ‘BEGIN { x = 5; x += 3; print “x =”, x }’
# Output:
# x = 8
Relational Operators
Used for comparing values, these operators form the basis of conditional logic. They include < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to), == (is equal to), and != (is not equal to).
[jerry]$ awk ‘$4 > 85 {print $2, “scored above 85”}’ marks.txt
# Output:
# Rahul scored above 85
# Shyam scored above 85
# Hari scored above 85
Logical Operators
These operators combine conditional expressions: && (logical AND), || (logical OR), and ! (logical NOT).
[jerry]$ awk ‘$4 > 85 && $3 == “Maths” {print $2, “is a top Maths student.”}’ marks.txt
# Output:
# Rahul is a top Maths student.
Ternary Operator
This operator provides a concise way to write a conditional expression: condition ? expr1 : expr2. If condition is true, the expression evaluates to expr1; otherwise, it evaluates to expr2.
[jerry]$ awk ‘{print $2, $4, ($4 > 85 ? “Pass” : “Fail”)}’ marks.txt
# Output:
# Amit 80 Fail
# Rahul 90 Pass
# Shyam 87 Pass
# Kedar 85 Fail
# Hari 89 Pass
Unary Operators
The unary + and – operators are used to specify the sign of a numeric literal.
Exponential Operators
The ^ and ** operators are used for exponentiation (raising a number to a power).
[jerry]$ awk ‘BEGIN { print “3^2 =”, 3^2 }’
# Output:
# 3^2 = 9
String Concatenation Operator
In AWK, a space between two string expressions serves as the concatenation operator, joining them into a single string.
[jerry]$ awk ‘{print “Student: ” $2 “, Subject: ” $3}’ marks.txt
# Output:
# Student: Amit, Subject: Physics
# Student: Rahul, Subject: Maths
# …
Array Membership Operator
The in keyword is an operator used to check if a specific index exists within an array.
[jerry]$ awk ‘BEGIN { grades[“Amit”]=80; if (“Amit” in grades) print “Amit has a grade.” }’
# Output:
# Amit has a grade.
Regular Expression Operators
The matching (~) and non-matching (!~) operators are used to test whether a field or variable matches a regular expression.
[jerry]$ awk ‘$2 ~ /^A/ {print $0}’ marks.txt
# Output:
# 1) Amit Physics 80
The regular expression operators are fundamental to AWK’s text processing capabilities, leading us to a more detailed examination of regular expressions themselves.