copied to clipboard!
string awk

A Beginner’s Guide to Text Processing using awk and if

updated: 2026/05/18 created: 2026/04/22

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

ElementDescription
$1First column (fruit name)
$2Second column (number)
if ($2 > 150)Checks if the second column is greater than 150
printOutputs 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

TypeOperatorMeaningExampleDescription
Comparison>=Greater than or equal$2 >= 20Second column (age) is 20 or more
Comparison<Less than$2 < 20Less than 20
Logical&&AND$2 >= 20 && $2 <= 30Both conditions met
Logical||OR$2 < 20 || $2 > 30Either 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

ElementContentDescription
$1NameGets first column data
$2ScoreGets second column data
if ($2 >= 70)ConditionChecks if score is 70 or more
printOutputDisplays string based on condition
elseBranchHandles 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

ExpressionMeaningDescription
$1NameGets first column data
$2ScoreGets second column data
if ($2 >= 80)Condition 1Checks for 80 or more
else if ($2 >= 70)Condition 2Checks for 70 or more
else if ($2 >= 50)Condition 3Checks for 50 or more
elseFinal branchHandles all other cases
printOutputDisplays 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

KeywordScopeBehaviorUse Case
nextPer lineSkips current line and moves to nextWhen you want to ignore specific lines
exitEntire programCompletely stops processingWhen you want to stop immediately upon condition
nextfilePer fileSkips current file and moves to nextWhen 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

ElementContent
$1First column (word portion)
~Regular expression match operator
/^apple/Regular expression meaning "starts with apple"
if (...)Processes only matching lines
print $0Outputs 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

ConditionTarget DataEvaluationResult Difference
$2 == ""bananaUndefined field → treated as empty stringJudged as empty
$2 == ""grape ""Exists as a stringNot judged as empty
$2+0 == 0bananaUndefined → converted to 0Judged as zero
$2+0 == 0orange 0Numeric 0Judged as zero
$2+0 == 0grape ""Empty string → converted to 0 numericallyJudged 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

ElementContentExecution Timing
BEGINRuns once before processing startsBefore input is read
if statementBranches processing based on conditionPer line
$1, $2Field (column) referencesDuring per-line processing
ENDRuns once after processing endsAfter 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 TypeConditionMethodBasisExample Result
Numeric$1 > 10Compared as numbersNumeric magnitudeOnly 30 passes
String$1 > "10"Compared as stringsDictionary 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

Itemawk ifShell if
Execution locationInside awkShell
Data processingAutomatic per-line processingRequires a loop
Syntax{ if (condition) action }if [ condition ]; then ... fi
VariablesField-based like $1Variables like $line
Use caseSpecialized for text processingGeneral 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

ItemContent
$1First column (name)
$2Second column (score)
if statementBranches processing based on condition
>= 60Pass/fail condition
?:Ternary operator (condition ? true : false)
printOutputs 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

ItemContent
Variable declarationNo prior declaration needed in awk; variables are created automatically when used
ScopeShared across the entire script by default (not block-local)
Variables inside ifEven if assigned inside an if statement, they can be referenced outside
When unassignedDefault value is empty string (0 in numeric context)
OverwritingValues 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

ItemPattern Matchingif Statement
PositionWritten directly in the pattern sectionWritten inside the action block
Syntax$1 == "apple" { action }{ if (condition) action }
Execution timingAction runs only when condition is trueBlock always runs, condition evaluated inside
ReadabilitySimple and shortSuited for complex conditions
ExtensibilitySomewhat limitedHigh (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

ElementContent
$2 > 150Checks if the value in column 2 is greater than 150
{ print $1 }Outputs column 1 of lines that meet the condition
Pattern formWriting the condition directly causes it to execute when true
if formAn 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

ElementContent
$1Product name (key)
$2Number (value to add)
price[$1] += $2Stores cumulative total in array
for (item in price)Loops through array
if (price[item] > 250)Filters with conditional branching
printOutputs 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

ItemCorrectIncorrectDescription
Comparison operator===== is comparison, = is assignment
Condition$2 > 150$2 = 150Result differs between numeric comparison and assignment
Outputprint $1SameExecutes 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

ElementContentPurpose
$1, $2Fields (column 1 and 2)Data reference
if ($2 > 150)Conditional branchingDetermines which lines match
printOutput processingDisplays results and debug info
"DEBUG:"Debug stringChecks progress during execution
"MATCH:"Marker when condition is metConfirms 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

ElementDescription
$1First column (name)
$2Second column (score)
if ($2 >= 60)Condition check (60 or more is a pass)
printOutput the result
{}Makes processing blocks clear
Line breaks and indentationImproves 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.

Mastering the awk Command

Leave a Reply

Your email address will not be published. Required fields are marked *

©︎ 2025-2026 running terminal commands