A clear explanation of the Tenth Line shell problem using awk, sed, head, and tail.
Problem Restatement
We are given a text file named file.txt.
We need to print only the tenth line of the file.
If the file has fewer than ten lines, the script should print nothing.
Example input:
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10Expected output:
Line 10Input and Output
| Item | Meaning |
|---|---|
| Input | A file named file.txt |
| Output | The tenth line only |
| Missing tenth line | Print nothing |
| Language | Bash shell script |
Examples
Input:
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10Output:
Line 10If the file contains only three lines:
Line 1
Line 2
Line 3The output is empty, because there is no tenth line.
First Thought
We only care about one line.
So we do not need to process or transform the whole file.
We need a command that can identify line number 10 and print exactly that line.
Several Unix tools can do this:
| Tool | Idea |
|---|---|
awk | Print the line where NR == 10 |
sed | Print only line 10 |
head and tail | Keep the first 10 lines, then take the last one |
Key Insight
In awk, NR means the current record number.
For normal text files, each record is one line.
So the tenth line is exactly the line where:
NR == 10When this condition is true, awk prints the current line.
Algorithm
Read the file line by line.
For each line:
- Track its line number.
- If the line number is
10, print it. - Ignore all other lines.
With awk, this becomes one command.
Correctness
awk processes the file one line at a time.
For each line, NR stores the current line number.
The condition NR == 10 is true only for the tenth line.
Therefore, awk 'NR == 10' file.txt prints the tenth line and no other line.
If the file has fewer than ten lines, the condition is never true, so nothing is printed.
Complexity
Let n be the number of lines in the file.
| Metric | Value | Why |
|---|---|---|
| Time | O(n) | In the simple form, awk may scan through the file |
| Space | O(1) | It processes one line at a time |
Implementation
# Read from the file file.txt and output the tenth line to stdout.
awk 'NR == 10' file.txtCode Explanation
The command is:
awk 'NR == 10' file.txtawk reads file.txt line by line.
NR is the current line number.
When NR equals 10, the condition is true.
In awk, when a condition is true and no explicit action is given, the default action is to print the current line.
So this command prints only line ten.
Alternative Implementations
Using sed:
sed -n '10p' file.txtThe -n option disables automatic printing.
The command 10p prints only line 10.
Using head and tail:
head -n 10 file.txt | tail -n 1head -n 10 keeps the first ten lines.
tail -n 1 prints the last line from those ten lines.
If the file has at least ten lines, that line is the tenth line.
Testing
Create a sample file:
cat > file.txt << 'EOF'
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
EOFRun the solution:
awk 'NR == 10' file.txtExpected output:
Line 10Test a file with fewer than ten lines:
cat > file.txt << 'EOF'
Line 1
Line 2
Line 3
EOFExpected output is empty.
Test a file with more than ten lines:
cat > file.txt << 'EOF'
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Line 11
EOFExpected output:
Line 10