Introduction
awk is a powerful command specialized for text processing, frequently used in log analysis and data manipulation.
Among its features, conditional branching with if is an important element that enhances processing flexibility.
However, beginners often find themselves confused by the differences from shell script if statements and awk's unique syntax.
This article carefully explains awk's conditional branching while highlighting common points of confusion.
Reference: awk manual
Basic awk Syntax
Creating the File
cat << 'EOF' > input.txt
apple 100
banana 200
cherry 150
EOF
Command
awk '{ if ($2 > 150) print $1, $2 }' input.txt
Output
banana 200
How It Works
| Element | Description |
|---|---|
| $1 | First column (fruit name) |
| $2 | Second column (number) |
| if ($2 > 150) | Checks if the second column is greater than 150 |
| Outputs lines that meet the condition |
Explanation
awk automatically splits each line and processes it according to conditions.
In this example, only lines where the number is greater than 150 are extracted.
Writing Conditions (Comparison and Logical Operators)
Creating the File
cat << 'EOF' > input.txt
Alice 25
Bob 17
Charlie 30
EOF
Command
awk '$2 >= 20 { print $1, $2 }' input.txt
Output
Alice 25
Charlie 30
Command
awk '$2 >= 20 && $2 <= 30 { print $1, $2 }' input.txt
Output
Alice 25
Charlie 30
Command
awk '$2 < 20 || $2 > 30 { print $1, $2 }' input.txt
Output
Bob 17
How It Works
| Type | Operator | Meaning | Example | Description |
|---|---|---|---|---|
| Comparison | >= | Greater than or equal | $2 >= 20 | Second column (age) is 20 or more |
| Comparison | < | Less than | $2 < 20 | Less than 20 |
| Logical | && | AND | $2 >= 20 && $2 <= 30 | Both conditions met |
| Logical | || | OR | $2 < 20 || $2 > 30 | Either condition met |
Explanation
In awk, columns are specified with $1, $2, etc., and only rows where the condition is true are processed.
Combining comparison and logical operators allows for flexible extraction.
Using else
Creating the File
cat << 'EOF' > input.txt
Alice 85
Bob 60
Charlie 45
EOF
Command
awk '{ if ($2 >= 70) { print $1 " : Pass" } else { print $1 " : Fail" } }' input.txt
Output
Alice : Pass
Bob : Fail
Charlie : Fail
How It Works
| Element | Content | Description |
|---|---|---|
| $1 | Name | Gets first column data |
| $2 | Score | Gets second column data |
| if ($2 >= 70) | Condition | Checks if score is 70 or more |
| Output | Displays string based on condition | |
| else | Branch | Handles cases where condition is not met |
Explanation
awk processes data line by line and can branch by column.
Using if-else allows for flexible output based on data.
Writing else if (Multi-way Branching)
Creating the File
cat << 'EOF' > input.txt
Alice 85
Bob 60
Charlie 45
David 70
EOF
Command
awk '{ if ($2 >= 80) { print $1 " : Excellent" } else if ($2 >= 70) { print $1 " : Good" } else if ($2 >= 50) { print $1 " : Pass" } else { print $1 " : Fail" } }' input.txt
Output
Alice : Excellent
Bob : Pass
Charlie : Fail
David : Good
How It Works
| Expression | Meaning | Description |
|---|---|---|
| $1 | Name | Gets first column data |
| $2 | Score | Gets second column data |
| if ($2 >= 80) | Condition 1 | Checks for 80 or more |
| else if ($2 >= 70) | Condition 2 | Checks for 70 or more |
| else if ($2 >= 50) | Condition 3 | Checks for 50 or more |
| else | Final branch | Handles all other cases |
| Output | Displays string based on condition |
Explanation
In awk, using else if allows multiple conditions to be evaluated in sequence.
Since conditions are evaluated from top to bottom, the order of statements affects the result.
How to Use exit, next, and nextfile
Creating the File
cat << 'EOF' > input.txt
apple 10
banana 20
skip 0
orange 30
stop 0
grape 40
EOF
Creating the File
cat << 'EOF' > input2.txt
melon 50
skip 0
peach 60
EOF
Command
awk '{ if ($1 == "skip") next; print }' input.txt
Output
apple 10
banana 20
orange 30
stop 0
grape 40
Command
awk '{ if ($1 == "stop") exit; print }' input.txt
Output
apple 10
banana 20
skip 0
orange 30
Command
awk '{ if ($1 == "skip") nextfile; print }' input.txt input2.txt
Output
apple 10
banana 20
melon 50
How It Works
| Keyword | Scope | Behavior | Use Case |
|---|---|---|---|
| next | Per line | Skips current line and moves to next | When you want to ignore specific lines |
| exit | Entire program | Completely stops processing | When you want to stop immediately upon condition |
| nextfile | Per file | Skips current file and moves to next | When you want to skip entire files |
Explanation
next "skips only that line", exit "terminates completely", and nextfile "skips at the file level" — these differ in granularity.
Using them appropriately enables efficient text processing.
Conditions Using Regular Expressions
Creating the File
cat << 'EOF' > input.txt
apple 100
banana 200
cherry 150
apple pie 300
EOF
Command
awk '{ if ($1 ~ /^apple/) print $0 }' input.txt
Output
apple 100
apple pie 300
How It Works
| Element | Content |
|---|---|
| $1 | First column (word portion) |
| ~ | Regular expression match operator |
| /^apple/ | Regular expression meaning "starts with apple" |
| if (...) | Processes only matching lines |
| print $0 | Outputs the entire line |
Explanation
In awk, using ~ allows conditional branching with regular expressions.
In this example, only lines that "start with apple" are extracted.
You can also omit the if and write it as follows.
awk '$1 ~ /^apple/ { print $0 }' input.txt
Handling Empty Values and Undefined Variables
Creating the File
cat << 'EOF' > input.txt
apple 100
banana
orange 0
grape ""
melon 50
EOF
Command
awk '{ if ($2 == "" ) print $1 ": empty or undefined"; else print $1 ": " $2 }' input.txt
Output
apple: 100
banana: empty or undefined
orange: 0
grape: ""
melon: 50
Command
awk '{ if ($2+0 == 0) print $1 ": zero or empty"; else print $1 ": " $2 }' input.txt
Output
apple: 100
banana: zero or empty
orange: zero or empty
grape: zero or empty
melon: 50
How It Works
| Condition | Target Data | Evaluation | Result Difference |
|---|---|---|---|
| $2 == "" | banana | Undefined field → treated as empty string | Judged as empty |
| $2 == "" | grape "" | Exists as a string | Not judged as empty |
| $2+0 == 0 | banana | Undefined → converted to 0 | Judged as zero |
| $2+0 == 0 | orange 0 | Numeric 0 | Judged as zero |
| $2+0 == 0 | grape "" | Empty string → converted to 0 numerically | Judged as zero |
Explanation
In awk, undefined fields are treated as empty strings, and in numeric operations they are converted to 0.
Therefore, care is needed because the result differs depending on whether you are checking "is it empty" or "is it numerically zero".
Using if in BEGIN and END Blocks
Creating the File
cat << 'EOF' > input.txt
apple 100
banana 200
orange 150
EOF
Command
awk 'BEGIN { print "=== START ===" } { if ($2 > 150) print $1 } END { print "=== END ===" }' input.txt
Output
=== START ===
banana
=== END ===
How It Works
| Element | Content | Execution Timing |
|---|---|---|
| BEGIN | Runs once before processing starts | Before input is read |
| if statement | Branches processing based on condition | Per line |
| $1, $2 | Field (column) references | During per-line processing |
| END | Runs once after processing ends | After all lines are processed |
Explanation
awk processes line by line, and BEGIN and END define pre- and post-processing.
Using if statements allows flexible extraction of only data that meets conditions.
Differences Between Numeric and String Comparison
Creating the File
cat << 'EOF' > input.txt
10 apple
2 banana
30 cherry
EOF
Command
awk '$1 > 10 {print $0}' input.txt
Output
30 cherry
Command
awk '$1 > "10" {print $0}' input.txt
Output
2 banana
30 cherry
How It Works
| Comparison Type | Condition | Method | Basis | Example Result |
|---|---|---|---|---|
| Numeric | $1 > 10 | Compared as numbers | Numeric magnitude | Only 30 passes |
| String | $1 > "10" | Compared as strings | Dictionary order (ASCII) | 2 and 30 pass |
Explanation
In awk, using a numeric literal in a comparison results in numeric comparison, while using a string results in string comparison.
Note that string comparison uses dictionary order, which can produce different results from numeric magnitude comparison.
Differences from if in Shell Scripts
Creating the File
cat << 'EOF' > input.txt
10
20
30
EOF
Command
awk '{ if ($1 > 15) print $1 }' input.txt
Output
20
30
Command
while read line; do
if [ "$line" -gt 15 ]; then
echo "$line"
fi
done < input.txt
Output
20
30
How It Works
| Item | awk if | Shell if |
|---|---|---|
| Execution location | Inside awk | Shell |
| Data processing | Automatic per-line processing | Requires a loop |
| Syntax | { if (condition) action } | if [ condition ]; then ... fi |
| Variables | Field-based like $1 | Variables like $line |
| Use case | Specialized for text processing | General control flow |
Explanation
awk is specialized for text processing and can be written concisely.
Shell if is flexible but requires additional constructs like loops.
Using the Ternary Operator (?:)
Creating the File
cat << 'EOF' > input.txt
Alice 80
Bob 45
Charlie 60
EOF
Command
awk '{ if ($2 >= 60) print $1, "Pass"; else print $1, "Fail" }' input.txt
Output
Alice Pass
Bob Fail
Charlie Pass
Command
awk '{ print $1, ($2 >= 60 ? "Pass" : "Fail") }' input.txt
Output
Alice Pass
Bob Fail
Charlie Pass
How It Works
| Item | Content |
|---|---|
| $1 | First column (name) |
| $2 | Second column (score) |
| if statement | Branches processing based on condition |
| >= 60 | Pass/fail condition |
| ?: | Ternary operator (condition ? true : false) |
| Outputs the result |
Explanation
The if statement provides explicit branching, while the ternary operator allows concise conditional branching.
Choose between them based on readability for the situation.
Variable Handling and Scope
Creating the File
cat << 'EOF' > input.txt
10
20
30
EOF
Command
awk '{ if ($1 > 15) { x = $1 * 2 } print $1, x }' input.txt
Output
10
20 40
30 60
Command
awk '{ if ($1 > 15) { y = $1 * 2 } else { y = 0 } print $1, y }' input.txt
Output
10 0
20 40
30 60
How It Works
| Item | Content |
|---|---|
| Variable declaration | No prior declaration needed in awk; variables are created automatically when used |
| Scope | Shared across the entire script by default (not block-local) |
| Variables inside if | Even if assigned inside an if statement, they can be referenced outside |
| When unassigned | Default value is empty string (0 in numeric context) |
| Overwriting | Values continue to be updated based on conditions |
Explanation
awk variables have no block scope; variables defined inside an if statement are accessible outside.
Care is needed because unassigned variables are treated as empty (or 0).
Difference Between Pattern Matching and Conditional Branching
Creating the File
cat << 'EOF' > input.txt
apple 100
banana 200
apple 300
orange 150
EOF
Command
awk '$1 == "apple" { print $0 }' input.txt
Output
apple 100
apple 300
Command
awk '{ if ($1 == "apple") print $0 }' input.txt
Output
apple 100
apple 300
How It Works
| Item | Pattern Matching | if Statement |
|---|---|---|
| Position | Written directly in the pattern section | Written inside the action block |
| Syntax | $1 == "apple" { action } | { if (condition) action } |
| Execution timing | Action runs only when condition is true | Block always runs, condition evaluated inside |
| Readability | Simple and short | Suited for complex conditions |
| Extensibility | Somewhat limited | High (can use else, etc.) |
Explanation
Pattern matching is concise and well-suited for simple conditions.
On the other hand, if statements are flexible and better for complex branching logic.
Writing One-liners
Creating the File
cat << 'EOF' > input.txt
apple 100
banana 200
orange 150
grape 300
EOF
Command
awk '$2 > 150 { print $1 }' input.txt
Output
banana
grape
Command
awk '{ if ($2 > 150) print $1 }' input.txt
Output
banana
grape
How It Works
| Element | Content |
|---|---|
| $2 > 150 | Checks if the value in column 2 is greater than 150 |
| { print $1 } | Outputs column 1 of lines that meet the condition |
| Pattern form | Writing the condition directly causes it to execute when true |
| if form | An explicit way to write conditional branching |
Explanation
In awk, writing the condition directly can concisely replace an if statement.
For complex conditions, using if improves readability.
Conditional Branching Combined with Arrays
Creating the File
cat << 'EOF' > input.txt
apple 100
banana 200
orange 150
apple 120
banana 180
EOF
Command
awk '{
price[$1] += $2
}
END {
for (item in price) {
if (price[item] > 250) {
print item, price[item]
}
}
}' input.txt
Output
banana 380
How It Works
| Element | Content |
|---|---|
| $1 | Product name (key) |
| $2 | Number (value to add) |
| price[$1] += $2 | Stores cumulative total in array |
| for (item in price) | Loops through array |
| if (price[item] > 250) | Filters with conditional branching |
| Outputs data matching the condition |
Explanation
This uses an awk array to aggregate data, then uses an if statement in the END block to extract only elements that meet the condition.
Flexible conditional branching is possible against the total value per key.
The array name and the variable used as a key can be changed to any string.
Common Errors in Conditional Branching and How to Fix Them
Creating the File
cat << 'EOF' > input.txt
apple 100
banana 200
orange 150
EOF
Command
awk '{ if ($2 > 150) print $1 }' input.txt
Output
banana
Command
awk '{ if ($2 = 150) print $1 }' input.txt
Output
apple
banana
orange
How It Works
| Item | Correct | Incorrect | Description |
|---|---|---|---|
| Comparison operator | == | = | == is comparison, = is assignment |
| Condition | $2 > 150 | $2 = 150 | Result differs between numeric comparison and assignment |
| Output | print $1 | Same | Executes only when condition is true |
Explanation
In awk, = is an assignment, so using it in a condition expression tends to always evaluate as true.
You must always use == for comparisons.
Debugging (Using Print Debug, etc.)
Creating the File
cat << 'EOF' > input.txt
apple 100
banana 200
orange 150
EOF
Command
awk '{ if ($2 > 150) print $1 }' input.txt
Output
banana
Command
awk '{ print "DEBUG:", $1, $2; if ($2 > 150) print "MATCH:", $1 }' input.txt
Output
DEBUG: apple 100
DEBUG: banana 200
MATCH: banana
DEBUG: orange 150
How It Works
| Element | Content | Purpose |
|---|---|---|
| $1, $2 | Fields (column 1 and 2) | Data reference |
| if ($2 > 150) | Conditional branching | Determines which lines match |
| Output processing | Displays results and debug info | |
| "DEBUG:" | Debug string | Checks progress during execution |
| "MATCH:" | Marker when condition is met | Confirms condition is satisfied |
Explanation
By using print to output intermediate values, you can visualize how conditional branching behaves.
Since awk has no step execution, this is the fundamental debugging approach.
Writing for Readability (Coding Conventions)
Creating the File
cat << 'EOF' > input.txt
Alice 85
Bob 45
Charlie 72
David 30
EOF
Command
awk '{if ($2 >= 60) print $1,"Pass"; else print $1, "Fail"}' input.txt
Output
Alice Pass
Bob Fail
Charlie Pass
David Fail
Command
awk '{
if ($2 >= 60) {
print $1, "Pass"
} else {
print $1, "Fail"
}
}' input.txt
Output
Alice Pass
Bob Fail
Charlie Pass
David Fail
How It Works
| Element | Description |
|---|---|
| $1 | First column (name) |
| $2 | Second column (score) |
| if ($2 >= 60) | Condition check (60 or more is a pass) |
| Output the result | |
| {} | Makes processing blocks clear |
| Line breaks and indentation | Improves readability (important as a convention) |
Explanation
Using line breaks and blocks in awk if statements makes the relationship between conditions and actions clear.
Even if a shorter version is possible, formatted code is recommended for maintainability.
Practical Tips for Mastering awk and if
The combination of awk and if is simple yet extremely powerful.
By understanding the difference between basic syntax and conditional branching and practicing with real-world examples, you will gain solid mastery.
In particular, being mindful of the distinction between pattern matching and if statements, as well as the difference between numeric and string comparisons, is the shortcut to improvement.
Start with short one-liners and gradually work up to more complex processing to deepen your understanding.
Articles on how to use awk other than with the “if”
The following link is an article about the awk command.
Please make use of it if you want to learn comprehensively.
