Bash tool to get nth line from a file
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Bash (Bourne Again SHell) is one of the most common shells in Unix-based systems, renowned for its powerful tools for processing text via command-line utilities. One common task you might encounter is extracting a specific line from a file. While there are multiple tools and methods to achieve this, in this article, we dive into convenient command-line methods using popular utilities like sed, awk, and a combination with others like head and tail.
Extracting the nth Line Using sed
The sed (stream editor) utility is designed for filtering and transforming text. To extract the nth line from a file with sed, you can use the following command:
Here, -n suppresses the default output, and '10p' instructs sed to print only the 10th line. Only the specified line will be output, making this method both simple and efficient for large files as sed stops reading the file after the required line number is reached.
Extracting the nth Line Using awk
awk is a potent pattern scanning and processing language. To use awk for extracting the nth line:
NR is an internal awk variable that keeps track of the number of records processed (in this case, lines). When NR equals 10, the line is printed. This method has similar efficiency to sed.
Using a Combination of head and tail
Another approach is the combination of head and tail. Although this is generally less efficient especially for large files because head outputs the first N lines, and tail extracts from that output:
This pipeline commands first get the top 10 lines of the file using head, and then tail takes only the last line from this output, which corresponds to the 10th line of the original file.
Comparative Efficiency
When working with large files, efficiency can vary significantly between methods. sed and awk are generally preferred for their direct access capabilities where they stop processing once the line is reached, unlike the head and tail combination which processes more data.
| Method | Command Example | Efficiency | |
sed | sed -n '10p' filename | High (stops after the line is found) | |
awk | awk 'NR==10' filename | High (stops after the line is found) | |
head and tail | `head -n 10 filename \ | tail -n 1` | Lower (processes more than necessary) |
Practical Application and Considerations
When choosing a method, consider the file size and the position of the line you're interested in. For large files where the line is near the start, differences in performance might be negligible, but this can change drastically if the line is toward the end, or if the operation needs to be performed multiple times on different parts of the file.
In scripting, where efficiency and speed are crucial, prefer sed or awk. For simpler, one-off commands, any method may suffice.
Summary
Extracting a specific line from a file is a frequent necessity in data processing and administration tasks. Bash provides multiple tools to perform this efficiently, with each having its strengths depending on the context of the task. Mastery of these tools enhances one's ability to manipulate and process files effectively directly from the command line.

