deep dive NR and FNR in linux awk command
Your time is limited, don’t waste it living someone else’s life. - Steve Jobs
deep dive NR awk commandPermalink
Mastering awk
and NR
: Unlocking the Power of Line Processing in LinuxPermalink
In the world of Linux system administration and Site Reliability Engineering (SRE), efficient text processing is essential. One of the most powerful tools for this is awk
, and at the heart of its capabilities is the NR
variable. This guide explores how to leverage NR
for advanced text processing, log analysis, and automation.
Understanding NR
in awk
Permalink
NR
(Number of Records) is a built-in awk
variable that represents the current line number being processed. It starts at 1
and increments with each new line of input.
Basic Usage of NR
Permalink
To print each line along with its line number:
awk '{print NR, $0}' file.txt
Example Input (file.txt):Permalink
apple
banana
cherry
date
elderberry
Output:Permalink
1 apple
2 banana
3 cherry
4 date
5 elderberry
Filtering with NR
Permalink
One of the most powerful use cases of NR
is filtering lines based on their number.
Print the 5th LinePermalink
awk 'NR==5' file.txt
Output:
elderberry
Print Lines from 5 to 10Permalink
awk 'NR>=5 && NR<=10' file.txt
Skip the First 5 Lines (Equivalent to tail -n +6
)Permalink
awk 'NR>5' file.txt
Practical SRE Use CasesPermalink
Log Monitoring: Show the Last 10 LinesPermalink
Equivalent to tail -n 10:
awk 'NR>(NR-10)' log.txt
Filtering Specific EventsPermalink
Extracts the first field (timestamp) of error logs from the 50th line onward:
awk 'NR>=50 && /ERROR/ {print $1}' app.log
NR
vs FNR
: Handling Multiple FilesPermalink
While NR
counts globally across all files, FNR
(File Number of Records) counts separately for each file.
Example: Printing Line Numbers for Multiple FilesPermalink
awk '{print FNR, NR, $0}' file1.txt file2.txt
If file1.txt
contains:Permalink
A1
A2
A3
And file2.txt
contains:Permalink
B1
B2
Output:Permalink
1 1 A1
2 2 A2
3 3 A3
1 4 B1
2 5 B2
FNR
resets forfile2.txt
, whileNR
continues counting.
Real-World Use Cases for SREs and DevelopersPermalink
1. Log File Analysis: Extracting Key InformationPermalink
To extract timestamps from an Apache log (only from the 100th line onward):
awk 'NR>=100 {print $4}' access.log
2. Automating Process MonitoringPermalink
To display only the username and process ID of the 5th running process:
ps aux | awk 'NR==5 {print $1 ":" $2}'
3. Extracting the Last 10 Lines of a Log FilePermalink
Equivalent to tail -n 10
:
awk 'NR>(NR-10)' log.txt
Final ThoughtsPermalink
Understanding and leveraging NR
in awk
unlocks incredible possibilities for text processing, automation, and log analysis. Whether you’re troubleshooting servers or parsing structured data, awk
remains a must-know tool in any Linux user’s arsenal.
Stay tuned for more deep dives into Linux command-line mastery!