Operators in AWK
Master arithmetic, comparison, logical, regex, and assignment operators in AWK with practical pattern-action examples
Operators are the building blocks of every AWK expression — they drive the calculations inside action blocks and the conditions inside patterns. Because AWK borrows much of its operator syntax from C, anyone with experience in C, Java, or JavaScript will recognize most of it immediately, but AWK adds two operator categories that make it special: regular expression match operators and an unusual “no operator” string concatenation.
AWK is a dynamically and weakly typed language, which means values fluidly switch between strings and numbers depending on context. The operator you choose tells AWK which interpretation to apply: + forces a numeric context, while string concatenation forces a string context. Understanding this duality is the key to writing correct AWK.
This tutorial walks through each operator family, how AWK’s pattern-action paradigm uses operators inside both patterns and actions, and the operator-driven idioms that make AWK programs concise.
Arithmetic Operators
AWK supports the standard arithmetic operators, plus exponentiation. All numeric values are double-precision floating-point internally.
| Operator | Meaning | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division (float) | a / b |
% | Modulo | a % b |
^ | Exponentiation | a ^ b |
** | Exponentiation alt | a ** b |
- (unary) | Negation | -a |
+ (unary) | Force numeric | +a |
Create a file named arithmetic.awk:
| |
Assignment and Increment Operators
AWK supports compound assignment operators and the C-style increment/decrement operators in both prefix and postfix forms.
| Operator | Meaning |
|---|---|
= | Plain assignment |
+= | Add and assign |
-= | Subtract and assign |
*= | Multiply and assign |
/= | Divide and assign |
%= | Modulo and assign |
^= | Exponentiate and assign |
++ | Increment (pre/post) |
-- | Decrement (pre/post) |
Create a file named assignment.awk:
| |
Comparison Operators and the String/Number Duality
Comparison operators return 1 for true and 0 for false. The interesting twist: AWK chooses between numeric and string comparison based on the operands. If both look like numbers, AWK compares numerically; otherwise it compares as strings (lexicographically).
| Operator | Meaning |
|---|---|
== | Equal |
!= | Not equal |
< | Less than |
<= | Less than or equal |
> | Greater than |
>= | Greater or equal |
Create a file named comparison.awk:
| |
Logical Operators
AWK uses C-style logical operators with short-circuit evaluation. Any non-zero, non-empty value is truthy; 0 and "" are falsy.
| Operator | Meaning |
|---|---|
&& | Logical AND |
|| | Logical OR |
! | Logical NOT |
Create a file named logical.awk:
| |
A famous AWK gotcha: the string "0" is truthy because it is a non-empty string, even though the number 0 is falsy. When in doubt, force the type with +x (numeric) or x "" (string).
String Concatenation: The Invisible Operator
AWK has no explicit string concatenation operator — you simply place expressions next to each other. This is unusual and worth seeing in action.
Create a file named concatenation.awk:
| |
Regular Expression Match Operators
AWK is a text-processing language, so it elevates regex matching to first-class operators: ~ (matches) and !~ (does not match). These are the operators most often used in patterns.
Create a file named regex_ops.awk:
| |
Conditional (Ternary) Operator
AWK supports the C-style ternary ?: for inline conditional expressions.
Create a file named ternary.awk:
| |
Operator Precedence
When operators combine, precedence determines the order of evaluation. From highest to lowest, AWK’s most-used operators rank roughly:
( )grouping,$field reference++ --(pre/post)^ **exponentiation (right-associative)! +x -xunary* / %+ -- string concatenation (no symbol)
< <= > >= != ==~ !~&&||?:ternary= += -= *= /= %= ^=
Create a file named precedence.awk:
| |
Running with Docker
Run any of the examples above with the Alpine image bundled with BusyBox AWK:
| |
Expected Output
Output of arithmetic.awk:
a = 17 b = 5
a + b = 22
a - b = 12
a * b = 85
a / b = 3.4
a % b = 2
a ^ b = 1419857
-a = -17
10 / 3 = 3.33333
int(10/3) = 3
Output of assignment.awk:
start x = 10
x += 5 x = 15
x -= 3 x = 12
x *= 2 x = 24
x /= 4 x = 6
post y++ -> 1 after: 2
pre ++z -> 2 after: 2
counter starts at:
after counter++ : 1
Output of comparison.awk:
10 > 9 : 1
10 == 10.0 : 1
"10" > "9" : 0
"abc" < "abd" : 1
+s + 8 : 50
(n "") < "99": 1
result of 5>3: 1
Output of logical.awk:
Eligible to drive
Standard fare
safely skipped division
!0 = 1
!"" = 1
!"0" = 0
!1 = 0
Output of concatenation.awk:
Hello World!
answer=42
sum is 5
a b > 3 : 1
Output of regex_ops.awk:
info -> INFO user logged in
error -> ERROR disk full
other -> WARN cache miss
error -> ERROR connection refused
info -> INFO request handled
Output of ternary.awk:
-2 is negative
-1 is negative
0 is zero
1 is positive
2 is positive
score=87 grade=B
Output of precedence.awk:
2 ^ 3 ^ 2 = 512
2 + 3 * 4 = 14
result: 3
20
14
Key Concepts
- Division is always floating-point — use
int()to truncate toward zero when you need integer behavior. - Comparison is type-sensitive: AWK compares numerically when both operands look like numbers, lexicographically otherwise. Force the type with unary
+or by concatenating"". - String concatenation is invisible — adjacent expressions concatenate, with precedence between arithmetic and comparison. Wrap in parentheses when mixing.
"0"is truthy while0is falsy. The type-aware truthiness rule trips up almost everyone at least once.- Regex operators
~and!~are first-class and most often used in patterns:$3 ~ /^[0-9]+$/ { … }is idiomatic AWK. - Exponentiation
^is right-associative:2 ^ 3 ^ 2evaluates as2 ^ (3 ^ 2) = 512. - Uninitialized variables behave as
0in numeric context and""in string context, which makes counter idioms likecount[$1]++work without explicit initialization. - When in doubt, parenthesize — AWK’s precedence rules around concatenation and comparison are subtle, and parentheses cost nothing at runtime.
Comments
Loading comments...
Leave a Comment