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
Command
Output
1 apple
2 banana
3 cherry
Command
Output
banana
How It Works
| Element | Description |
|---|---|
| awk | Text processing tool (processes line by line) |
| NR | The line number currently being processed (starts from 1) |
| $0 | The entire content of the line |
| NR==2 | A 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
File Creation
Command
Output
1 1 A
2 2 B
3 3 C
4 1 D
5 2 E
How It Works
| Item | NR | FNR |
|---|---|---|
| Meaning | Cumulative line number across all files | Line number per individual file |
| Reset | Not reset | Reset per file |
| input1.txt | 1,2,3 | 1,2,3 |
| input2.txt | 4,5 | 1,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
Command
Output
cherry
Command
Output
banana
cherry
date
Command
Output
banana
date
fig
How It Works
| Element | Description |
|---|---|
| NR | The line number currently being processed (counted automatically) |
| NR==3 | Output only line 3 |
| NR>=2 && NR<=4 | Extract lines in the range of 2 to 4 |
| NR%2==0 | Output 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
Command
Output
3 cherry
6 fig
Command
Output
1 apple
4 date
7 grape
How It Works
| Element | Description |
|---|---|
| NR | Current line number (starts from 1) |
| NR % n | Remainder when line number is divided by n |
| Condition match | Output the line when the remainder equals a specific value |
| Application | Flexible 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
Command
Output
line2
line3
line4
Command
Output
line3
line4
line5
How It Works
| Syntax | Meaning | Behavior |
|---|---|---|
| NR | Current line number | Counted sequentially from line 1 |
| NR>=2 && NR<=4 | Range condition (logical operation) | Output only lines 2 to 4 |
| NR==3, NR==5 | Range specification (range operator) | Continuously output from line 3 to line 5 |
| && | AND condition | Only 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
Command
Output
Last line: 4 Total: 70
Command
Output
Count: 4 Min: 10 Max: 25
How It Works
| Element | Description |
|---|---|
| NR | The line number currently being processed (equals total line count at the last line) |
| $2 | The second column of each line (numeric data) |
| sum += $2 | Cumulative calculation of numbers |
| END | A block executed only once after all lines are processed |
| NR==1 | Initial value setup (executed on the first line) |
| min / max | Variables 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
File Creation
Command
Output
1 A1
2 A2
3 A3
4 B1
5 B2
Command
Output
1 A1
2 A2
3 A3
1 B1
2 B2
Command
Output
--- new file ---
1 1 A1
2 2 A2
3 3 A3
--- new file ---
4 1 B1
5 2 B2
How It Works
| Item | Meaning | Behavior When Crossing Files |
|---|---|---|
| NR | Cumulative line number across all input | Not reset |
| FNR | Line number per file | Reset per file |
| FNR==1 | Detection of the first line of a new file | Can 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
Command
Output
Alice,30,Tokyo
Bob,25,Osaka
Charlie,35,Nagoya
How It Works
| Element | Description |
|---|---|
| NR | The line number currently being processed (starts from 1) |
| NR>1 | Exclude line 1 (the header) and process only from line 2 onward |
| awk | A command that processes text line by line |
| input.txt | The 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
Command
Output
1 apple 100
2 banana 200
3 orange 150
Command
Output
2 banana 200
4 grape 300
5 melon 250
Command
Output
1 apple 100
3 orange 150
5 melon 250
How It Works
| Element | Meaning | Role |
|---|---|---|
| NR | Record number (line number) | Line-based condition specification |
| NF | Number of fields | Checking the number of columns |
| $1, $2 | Each field | Data extraction and comparison |
| Condition expression (NR <= 3, etc.) | Filter condition | Control of extraction targets |
| Logical operators (&&, %) | Combining conditions | Complex 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.

![[Shell script] Execute and understand; obtain error output from execution results. string shell script](https://running-terminal-commands.com/wp-content/uploads/thumbnail_shell-script_1920_1080.png.webp)
![[sed] Run and understand: Display the last line in a file string sed](https://running-terminal-commands.com/wp-content/uploads/thumbnail_sed_1920_1080-1.png.webp)