Tenth Line - Problem
Your mission: Extract the 10th line from a text file named
What you need to do:
โข Read from a file called
โข Print only the 10th line of that file
โข Handle cases where the file might have fewer than 10 lines
Example:
If
Your output should be:
file.txt. Sounds simple? It is! But there are several elegant ways to accomplish this task using shell commands.What you need to do:
โข Read from a file called
file.txtโข Print only the 10th line of that file
โข Handle cases where the file might have fewer than 10 lines
Example:
If
file.txt contains:Line 1
Line 2
...
Line 10
Line 11Your output should be:
Line 10 Input & Output
basic_file.txt
$
Input:
file.txt contains:
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Line 11
Line 12
โบ
Output:
Line 10
๐ก Note:
The file has more than 10 lines, so we successfully extract the 10th line
exactly_ten_lines.txt
$
Input:
file.txt contains:
First
Second
Third
Fourth
Fifth
Sixth
Seventh
Eighth
Ninth
Tenth
โบ
Output:
Tenth
๐ก Note:
The file has exactly 10 lines, so the 10th line is the last line
fewer_lines.txt
$
Input:
file.txt contains:
Only line 1
Only line 2
Only line 3
โบ
Output:
(no output)
๐ก Note:
The file has fewer than 10 lines, so there's no 10th line to print
Constraints
-
The file is named
file.txt - Each line contains at most 1000 characters
- The file may have anywhere from 0 to 106 lines
- Handle gracefully when file has fewer than 10 lines
Visualization
Tap to expand
Understanding the Visualization
1
File Structure
Text file consists of sequentially numbered lines
2
Line Addressing
Different tools offer various ways to target specific lines
3
Extraction
Output only the content of the 10th line
Key Takeaway
๐ฏ Key Insight: While all approaches work, `sed -n '10p'` provides the most efficient solution with direct line addressing, avoiding unnecessary data processing and offering the cleanest syntax for this specific task.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code