copied to clipboard!
string awk

Mastering AWK and NF: The Practical Guide to Seamless Data Processing

updated: 2026/05/05 created: 2026/05/02

Introduction

When you start learning text processing on the command line, many people encounter awk.

Awk is a tool capable of powerful data processing with simple syntax, but there are quite a few points where beginners tend to stumble.

One of them is understanding the special variable NF. NF may seem modest at first glance, but it plays a very important role in log analysis and data formatting in practical work.

This article carefully explains NF in awk from the basics to advanced usage, from the perspective of a beginner.

Rather than simply explaining commands, it is structured with an awareness of why that usage is the way it is and where people tend to get stuck. Let's deepen our understanding by following along with the flow.

Reference: GNU awk

Basic Definition and Role of the Special Variable “NF” in awk

Creating the File

cat << 'EOF' > input.txt apple orange banana cat dog red blue green yellow EOF

Command to Run

awk '{print NF}' input.txt

Output

3
2
4

Command to Run

awk '{print $NF}' input.txt

Output

banana
dog
yellow

Command to Run

awk '{print $1, $NF}' input.txt

Output

apple banana
cat dog
red yellow

How It Works

ItemDescription
NFA special variable that represents the number of fields (columns) in a record (one line)
$NFPoints to the last field (the NF-th field)
$1The first field
awk behaviorProcesses line by line and automatically splits fields (default delimiter is whitespace)
Use casesChecking column count, extracting the last column, processing variable-length data

Explanation

NF automatically holds the number of fields for each line, making it very convenient for processing variable-column data.
By using $NF, you can always retrieve the "last value" even when the number of columns differs.

How to Distinguish Between Referencing Field Count and Extracting the Last Column

Creating the File

cat << 'EOF' > input.txt apple orange banana cat dog red blue green yellow one EOF

Command to Run

awk '{print NF}' input.txt

Output

3
2
4
1

Command to Run

awk '{print $NF}' input.txt

Output

banana
dog
yellow
one

How It Works

ItemDescriptionExample ("apple orange banana")
NFBuilt-in variable representing the number of fields (columns)3
$NFRetrieves the last fieldbanana
$1~$nReferences each field by number$1=apple, $2=orange

Explanation

NF returns the number of columns per line, and $NF uses that column count as an index to retrieve the last column.
For data with variable column counts, you can process flexibly by using these two in combination.

How to Extract and Filter Only Rows with a Specific Number of Columns

Creating the File

cat << 'EOF' > input.txt a b c 1 2 x y z w hello world EOF

Command to Run

awk 'NF==3' input.txt

Output

a b c

Command to Run

awk 'NF==2' input.txt

Output

1 2
hello world

How It Works

ElementDescription
awkText processing tool
NFBuilt-in variable representing the number of fields (columns)
NF==3Extracts only rows with 3 columns
NF==2Extracts only rows with 2 columns
DelimiterDefault is whitespace (spaces and tabs)

Explanation

Because NF automatically counts the number of columns for each line, you can easily filter just by specifying a condition.
This is a basic technique commonly used in log formatting and invalid data detection.

Combining NF with Loop Processing (for Statement) to Dynamically Operate on All Columns

Creating the File

cat << 'EOF' > input.txt apple orange banana cat dog red blue green yellow EOF

Command to Run

awk '{print NF}' input.txt

Output

3
2
4

Command to Run

awk '{ for(i=1; i<=NF; i++){ print "Column" i ":" $i } }' input.txt

Output

Column1:apple
Column2:orange
Column3:banana
Column1:cat
Column2:dog
Column1:red
Column2:blue
Column3:green
Column4:yellow

How It Works

ElementDescription
NFHolds the number of fields (columns) in the current line
$iReferences the i-th field
for(i=1; i<=NF; i++)Loops from the 1st column to the last column
printDynamically outputs each field

Explanation

Using NF allows flexible processing even for data with different numbers of columns.
The key point is that combining it with a for statement lets you automatically scan through all columns.

The Effect of Changing the Delimiter (FS) on the NF Value and Points to Watch

Creating the File

cat << 'EOF' > input.txt apple orange banana dog,cat,bird one:two:three:four EOF

Command to Run

awk '{print $0 " -> NF=" NF}' input.txt

Output

apple orange banana -> NF=3
dog,cat,bird -> NF=1
one:two:three:four -> NF=1

Command to Run

awk -F',' '{print $0 " -> NF=" NF}' input.txt

Output

apple orange banana -> NF=1
dog,cat,bird -> NF=3
one:two:three:four -> NF=1

Command to Run

awk -F':' '{print $0 " -> NF=" NF}' input.txt

Output

apple orange banana -> NF=1
dog,cat,bird -> NF=1
one:two:three:four -> NF=4

How It Works

FS (Delimiter)Target LineSplit ResultNF
Default (whitespace)apple orange bananaapple / orange / banana3
Default (whitespace)dog,cat,birdTreated as 1 field1
,dog,cat,birddog / cat / bird3
:one:two:three:fourone / two / three / four4
, or :Lines with mismatched delimiterNot split, remains 1 field1

Explanation

Changing FS changes the splitting unit, so NF (the number of fields) also changes significantly.
Note that when the input data and FS do not match, NF will unintentionally become 1.

Techniques for Efficiently Analyzing Variable-Length Format Log Files with NF

Creating the File

cat << 'EOF' > input.txt INFO 2026-05-01 userA login success WARN 2026-05-01 userB disk almost_full ERROR 2026-05-01 userC DEBUG userD action=click button=submit extra=data EOF

Command to Run

awk '{print $0, "=> NF=" NF}' input.txt

Output

INFO 2026-05-01 userA login success => NF=5
WARN 2026-05-01 userB disk almost_full => NF=5
ERROR 2026-05-01 userC => NF=3
DEBUG userD action=click button=submit extra=data => NF=5

Command to Run

awk 'NF < 5 {print "Missing fields:", $0}' input.txt

Output

Missing fields: ERROR 2026-05-01 userC

Command to Run

awk '{print "Last field:", $NF}' input.txt

Output

Last field: success
Last field: almost_full
Last field: userC
Last field: extra=data

How It Works

ConceptDescriptionExample
NFNumber of fields per lineNF=5
$NFThe last fieldsuccess
NF condition判定 for variable-length dataNF < 5
Variable-length supportCan process even with different column countsError log extraction

Explanation

Using awk's NF allows flexible analysis even for logs with an undefined number of columns.
In particular, combining it with $NF lets you write last-field retrieval simply.

Validation Method Using NF with Conditional Branching (if Statement) to Exclude Incomplete Data Rows

Creating the File

cat << 'EOF' > input.txt apple orange banana grape melon peach kiwi EOF

Command to Run

awk '{ if (NF >= 2) print }' input.txt

Output

apple orange banana
melon peach

How It Works

ElementDescription
NFNumber of fields (column count separated by whitespace)
Conditional branchingif (NF >= 2) outputs only lines with 2 or more items
Exclusion targetsIncomplete lines with only 1 item (e.g., grape, kiwi)
Processing contentOnly prints lines that satisfy the condition with an if statement

Explanation

Using an if statement makes the intent of the condition clear, resulting in more readable validation processing.
NF's strength is that it can make judgments quickly and efficiently.

How to Subtract a Specific Column Count from NF to Specify the “nth from the End”

Creating the File

cat << 'EOF' > input.txt a b c d e 1 2 3 4 5 x y z EOF

Command to Run

awk '{print $(NF)}' input.txt

Output

e
5
z

Command to Run

awk '{print $(NF-1)}' input.txt

Output

d
4
y

Command to Run

awk '{print $(NF-2)}' input.txt

Output

c
3
x

How It Works

ExpressionMeaningExample (a b c d e)Output
NFTotal number of fields5e
NF-12nd from the end5-1=4d
NF-23rd from the end5-2=3c
$(NF-n)(n+1)th from the endvariableany

Explanation

NF represents the number of fields in a line, and by subtracting from it you can specify positions counting from the end.
Using the form $(NF-n), you can flexibly retrieve the column that is "n positions from the end."

Summary for Mastering awk and NF

NF in awk holds more meaning than just "number of columns."

It is a core concept in many situations, including retrieving the last column, checking data integrity, and handling variable-length data.

For beginners, the shortest path to understanding is to start with basic usage such as $NF and NF==number, then gradually combine them with for loops and conditional branching.

Leave a Reply

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

©︎ 2025-2026 running terminal commands