copied to clipboard!
string awk

Mastering awk: An Introductory Guide to NR and Row Manipulation

updated: 2026/05/03 created: 2026/05/03

Introduction

One of the commonly used commands in text processing is awk.
It is especially powerful when handling data line by line, such as in log analysis or CSV processing.

Among its features, the NR variable plays an important role. While it may appear to be just a line number, mastering it allows you to write extraction, aggregation, and filtering operations concisely.

This article systematically explains everything from the basic structure of awk, the role of NR, the difference from FNR, to practical data extraction techniques.

It is structured so that beginners can understand useful usage while also touching on common stumbling points.

Reference: GNU awk

Basic Structure of AWK and the Role of the NR Variable

File Creation

cat << 'EOF' > input.txt apple banana cherry EOF

Command

awk '{print NR, $0}' input.txt

Output

1 apple
2 banana
3 cherry

Command

awk 'NR==2 {print $0}' input.txt

Output

banana

How It Works

ElementDescription
awkText processing tool (processes line by line)
NRThe line number currently being processed (starts from 1)
$0The entire content of the line
NR==2A condition that specifies "process only line 2"

Explanation

NR is a variable that automatically counts line numbers and is used for conditional branching per line and numbered output.
The basic principle of awk is to "read one line at a time and apply conditions and actions."

Difference Between NR (Record Number) and FNR (Per-File Line Number)

File Creation

cat << 'EOF' > input1.txt A B C EOF

File Creation

cat << 'EOF' > input2.txt D E EOF

Command

awk '{print NR, FNR, $0}' input1.txt input2.txt

Output

1 1 A
2 2 B
3 3 C
4 1 D
5 2 E

How It Works

ItemNRFNR
MeaningCumulative line number across all filesLine number per individual file
ResetNot resetReset per file
input1.txt1,2,31,2,3
input2.txt4,51,2

Explanation

NR is the overall sequential number, while FNR is the per-file sequential number.
The key is to understand the difference when processing multiple files.

Techniques for Extracting Data by Specifying a Line Number

File Creation

cat << 'EOF' > input.txt apple banana cherry date elderberry fig grape EOF

Command

awk 'NR==3' input.txt

Output

cherry

Command

awk 'NR>=2 && NR<=4' input.txt

Output

banana
cherry
date

Command

awk 'NR%2==0' input.txt

Output

banana
date
fig

How It Works

ElementDescription
NRThe line number currently being processed (counted automatically)
NR==3Output only line 3
NR>=2 && NR<=4Extract lines in the range of 2 to 4
NR%2==0Output only even-numbered lines (remainder of line number divided by 2 is 0)

Explanation

NR is the basic variable for handling line numbers in awk, and combining it with conditional expressions enables flexible extraction.
Though simple, it is extremely powerful for log analysis and data processing.

Data Sampling Every N Lines Using NR

File Creation

cat << 'EOF' > input.txt 1 apple 2 banana 3 cherry 4 date 5 elderberry 6 fig 7 grape 8 honeydew EOF

Command

awk 'NR % 3 == 0' input.txt

Output

3 cherry
6 fig

Command

awk 'NR % 3 == 1' input.txt

Output

1 apple
4 date
7 grape

How It Works

ElementDescription
NRCurrent line number (starts from 1)
NR % nRemainder when line number is divided by n
Condition matchOutput the line when the remainder equals a specific value
ApplicationFlexible sampling by changing the starting position

Explanation

NR represents the line number, and combining it with modulo arithmetic enables periodic line extraction.
It is a simple yet powerful technique commonly used in log analysis and thinning-out processing.

Extracting a Specific Range of Lines Using Operators

File Creation

cat << 'EOF' > input.txt line1 line2 line3 line4 line5 line6 EOF

Command

awk 'NR>=2 && NR<=4' input.txt

Output

line2
line3
line4

Command

awk 'NR==3, NR==5' input.txt

Output

line3
line4
line5

How It Works

SyntaxMeaningBehavior
NRCurrent line numberCounted sequentially from line 1
NR>=2 && NR<=4Range condition (logical operation)Output only lines 2 to 4
NR==3, NR==5Range specification (range operator)Continuously output from line 3 to line 5
&&AND conditionOnly target lines that satisfy both conditions

Explanation

Using NR in awk allows you to flexibly extract line ranges with logical operations or range specifications.
In particular, NR==A,NR==B is convenient because it automatically outputs everything from the start to the end.

Identifying the Last Line and Computing Data Statistics Using NR and the END Block

File Creation

cat << 'EOF' > input.txt apple 10 banana 20 orange 15 grape 25 EOF

Command

awk '{ sum += $2 } END { print "Last line:", NR, "Total:", sum }' input.txt

Output

Last line: 4 Total: 70

Command

awk 'NR==1{min=$2; max=$2} { if($2max) max=$2 } END{ print "Count:", NR, "Min:", min, "Max:", max }' input.txt

Output

Count: 4 Min: 10 Max: 25

How It Works

ElementDescription
NRThe line number currently being processed (equals total line count at the last line)
$2The second column of each line (numeric data)
sum += $2Cumulative calculation of numbers
ENDA block executed only once after all lines are processed
NR==1Initial value setup (executed on the first line)
min / maxVariables for updating minimum and maximum values

Explanation

NR functions as a line counter, and combining it with END allows you to naturally obtain the "last line = total count."
The strength of awk is that statistical processing can be completed in a single pass.

Behavior of NR When Processing Multiple Files and Points to Note

File Creation

cat << 'EOF' > file1.txt A1 A2 A3 EOF

File Creation

cat << 'EOF' > file2.txt B1 B2 EOF

Command

awk '{print NR, $0}' file1.txt file2.txt

Output

1 A1
2 A2
3 A3
4 B1
5 B2

Command

awk '{print FNR, $0}' file1.txt file2.txt

Output

1 A1
2 A2
3 A3
1 B1
2 B2

Command

awk 'FNR==1{print "--- new file ---"} {print NR, FNR, $0}' file1.txt file2.txt

Output

--- new file ---
1 1 A1
2 2 A2
3 3 A3
--- new file ---
4 1 B1
5 2 B2

How It Works

ItemMeaningBehavior When Crossing Files
NRCumulative line number across all inputNot reset
FNRLine number per fileReset per file
FNR==1Detection of the first line of a new fileCan be used to detect file transitions

Explanation

NR is a sequential number across all input, while FNR is a sequential number per file, so they need to be used appropriately depending on the purpose when processing multiple files.
In particular, FNR==1 is the standard way to detect file boundaries.

Simple CSV Header Skipping Using NR

File Creation

cat << 'EOF' > input.txt name,age,city Alice,30,Tokyo Bob,25,Osaka Charlie,35,Nagoya EOF

Command

awk 'NR>1' input.txt

Output

Alice,30,Tokyo
Bob,25,Osaka
Charlie,35,Nagoya

How It Works

ElementDescription
NRThe line number currently being processed (starts from 1)
NR>1Exclude line 1 (the header) and process only from line 2 onward
awkA command that processes text line by line
input.txtThe CSV file to be processed

Explanation

Using NR enables line-number-based control, making it simple to skip headers.
It is a lightweight and commonly used method for preprocessing CSV files.

Filtering by Combining NR with Other Built-in Variables Such as NF

File Creation

cat << 'EOF' > input.txt apple 100 banana 200 orange 150 grape 300 melon 250 EOF

Command

awk 'NR <= 3 {print NR, $0}' input.txt

Output

1 apple 100
2 banana 200
3 orange 150

Command

awk 'NF == 2 && $2 >= 200 {print NR, $1, $2}' input.txt

Output

2 banana 200
4 grape 300
5 melon 250

Command

awk 'NR % 2 == 1 && NF == 2 {print NR, $0}' input.txt

Output

1 apple 100
3 orange 150
5 melon 250

How It Works

ElementMeaningRole
NRRecord number (line number)Line-based condition specification
NFNumber of fieldsChecking the number of columns
$1, $2Each fieldData extraction and comparison
Condition expression (NR <= 3, etc.)Filter conditionControl of extraction targets
Logical operators (&&, %)Combining conditionsComplex filtering

Explanation

Combining NR for line control with NF and field values enables flexible extraction.
The strength of awk is being able to handle line numbers, column counts, and value conditions simultaneously.

Practical Tips for Mastering awk and NR

In awk, NR is not just a line number — it is an important element that serves as the axis for conditional branching and data extraction.

Understanding the difference from FNR and using each appropriately depending on whether you are working with a single file or multiple files is the foundation.

In addition, mastering typical patterns using NR — such as extracting specific lines, sampling every n lines, specifying ranges, and retrieving the last line — allows you to efficiently handle a wide range of everyday text processing tasks.

Furthermore, combining NR with other variables such as NF makes more practical data processing possible.

For beginners, it is recommended to start with the simple idea of "using NR as a condition."

From there, gradually combining it with operators and other variables will allow you to experience the true power of awk.

Leave a Reply

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

©︎ 2025-2026 running terminal commands