Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to read a Specific Line From a File in Linux?
When working with the Linux command line, reading specific lines from text files is a common task. Rather than viewing entire files, you often need to extract just one particular line based on its line number.
This article explores four different methods to read a specific line from a file using various Linux commands and utilities.
Sample File Setup
Let's create a sample file called test.txt to demonstrate these methods
$ nl test.txt
1 This is line 1, I don't have any interesting data.
2 This is line 2, I don't have any interesting data.
3 This is line 3, I don't have any interesting data.
4 This is line 4, I don't have any interesting data.
5 This is line 5, interesting data: Linux is awesome!
6 This is line 6, I don't have any interesting data.
7 This is line 7, I don't have any interesting data.
The nl command displays the file contents with line numbers. Our goal is to extract line 5, which contains the interesting data.
Method 1 Using Bash Script
You can create a bash script to read a specific line by iterating through the file and counting lines
#!/bin/bash
FILE="$1"
LINE_NO=$2
i=0
while read line; do
i=$(( i + 1 ))
test $i = $LINE_NO && echo "$line"
done
This script accepts two arguments: the filename and target line number. However, it processes the entire file. A more efficient version stops after finding the target line
#!/bin/bash
FILE="$1"
LINE_NO=$2
i=0
while read line; do
i=$(( i + 1 ))
case $i in $LINE_NO) echo "$line"; break;; esac
done
Usage
$ ./getLine.sh test.txt 5
This is line 5, interesting data: Linux is awesome!
Method 2 Using sed Command
The sed command provides compact one-liners for extracting specific lines. Here are two approaches
Delete All Except Target Line
$ sed '5!d' test.txt
Print Only Target Line
$ sed -n '5p' test.txt
This is line 5, interesting data: Linux is awesome!
For better performance on large files, add the q (quit) command to stop processing after finding the target line
$ sed '5!d;q' test.txt
$ sed -n '5{p;q}' test.txt
Method 3 Using awk Command
The awk command can extract a specific line using the built-in NR (Number of Records) variable
$ awk 'NR==5' test.txt
For better performance, exit after printing the target line
$ awk 'NR==5{ print; exit }' test.txt
This is line 5, interesting data: Linux is awesome!
Method 4 Using head and tail Commands
You can combine head and tail commands to extract a specific line. The strategy is
Use
head -n Xto get the first X linesPipe the output to
tail -1to get the last line from those X lines
$ head -n 5 test.txt | tail -1
This is line 5, interesting data: Linux is awesome!
Performance Comparison
| Method | Command | Performance | Best For |
|---|---|---|---|
| Bash Script |
while read loop |
Slower | Complex logic |
| sed | sed -n '5{p;q}' |
Fast | Large files |
| awk | awk 'NR==5{print;exit}' |
Fast | Text processing |
| head + tail | head -n 5 | tail -1 |
Moderate | Simple extraction |
Conclusion
Linux provides multiple ways to read specific lines from files. For large files, use sed or awk with quit commands for optimal performance. The head and tail combination offers a simple, readable solution for smaller files.
