Tenth Line - Problem

Given a text file file.txt, write a shell command or script to print just the 10th line of the file.

Note: If the file has fewer than 10 lines, the output should be empty (nothing printed).

Input & Output

Example 1 — File with 15 lines
$ Input: file.txt contains 15 lines, line 10 is "tenth line content"
Output: tenth line content
💡 Note: The file has more than 10 lines, so we successfully extract and print the 10th line
Example 2 — File with exactly 10 lines
$ Input: file.txt contains exactly 10 lines, line 10 is "last line"
Output: last line
💡 Note: The file has exactly 10 lines, so line 10 exists and is printed
Example 3 — File with fewer than 10 lines
$ Input: file.txt contains only 5 lines
Output:
💡 Note: The file has fewer than 10 lines, so there's no 10th line to print (empty output)

Constraints

  • The file may contain 0 to any number of lines
  • Each line can be up to 1000 characters
  • If file doesn't exist or has fewer than 10 lines, output should be empty

Visualization

Tap to expand
Tenth Line: Extract Specific Line from Filefile.txtLine 1: First lineLine 2: Second lineLine 3: Third lineLine 4: Fourth lineLine 5: Fifth lineLine 6: Sixth lineLine 7: Seventh lineLine 8: Eighth lineLine 9: Ninth lineLine 10: TARGET LINE ←Line 11: More contentShell CommandProcessingsed -n '10p'orawk 'NR==10'OutputTARGET LINE(10th line only)🎯 Extract exactly line 10, ignore all others
Understanding the Visualization
1
Input File
Text file with multiple lines, need line 10
2
Process
Use shell commands to locate 10th line
3
Output
Print only the 10th line content
Key Takeaway
🎯 Key Insight: Use Unix line addressing tools to directly access specific line numbers without reading/storing entire file content
Asked in
Unix Systems 30 DevOps 25
31.2K Views
Medium Frequency
~5 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen