Bash 0195.bash
# Read from the file file.txt and output the tenth line to stdout.
head -10 file.txt | sed -n 10p
Difficulty
and has an acceptance rate of about 37% (400k+ submissions).
Typical patterns to solve this problem:
- Shell
AI Disclaimer
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.
See this dedicated page for more details.
Code Walkthrough
Overview
The script is a compact Bash pipeline that extracts the tenth line from a text file named file.txt. It combines two classic Unix utilities-head and sed-to limit the amount of data read from the file and then select the desired line. The pipeline is executed in a single command line, making it suitable for quick ad-hoc inspections or inclusion in larger shell scripts.
Key Steps
The first stage uses head to read only the initial segment of the file:
head -10 file.txt
head -10 stops reading after the tenth line, ensuring that the subsequent command receives at most ten lines regardless of the file’s total size.
The second stage pipes the output of head into sed, which is instructed to suppress automatic printing and to emit only the tenth line:
sed -n 10p
The -n flag disables the default behavior of printing every processed line, while 10p tells sed to print the line whose address is 10. Because the input stream contains at most ten lines, the address matches only the final line of the stream.
The complete pipeline connects these two stages:
head -10 file.txt | sed -n 10p
Edge Cases
When the source file contains fewer than ten lines, head -10 outputs all available lines without error. The subsequent sed -n 10p receives a stream that does not include a tenth line, so the address does not match any input line and nothing is printed. This behavior gracefully handles short files by producing no output rather than raising an exception.
If the file includes lines that are empty, contain whitespace, or embed special characters, both head and sed treat them as ordinary text. The pipeline therefore returns the exact tenth line content, preserving any leading or trailing spaces and non-printing characters that may be present.
When the file is a regular text file with standard line endings, the pipeline works as expected. In the case of a file that uses a different line-ending convention (for example, carriage-return-line-feed), head still counts logical lines based on newline characters, and sed will output the line as it appears in the input stream.
Performance Optimizations
The use of head -10 is a deliberate optimization that limits I/O. By stopping the read operation after ten lines, the command avoids scanning the remainder of a potentially large file. This reduces disk access time and memory consumption, especially when the file resides on slower storage media.
Passing a truncated stream to sed further improves efficiency. The -n option prevents sed from performing its default line-by-line output, which would otherwise generate unnecessary data. The explicit address 10p ensures that sed evaluates only the line of interest, avoiding any additional processing for earlier lines.
Together, these choices create a pipeline that performs minimal work: it reads just enough data to locate the target line and emits only that line, making the command fast and lightweight even on very large files.
ELI5 - Simplified Explanation
Imagine you have a really tall stack of ten giant picture books sitting on your desk. Each page in those books is like a line of text in a computer file. This code is a little robot assistant that helps you find exactly what is on the tenth page of your stack. First, the robot uses the head command to grab the top ten books from the pile, which is like holding onto the first ten pages. Then, it uses the sed command to look at that small pile and pick out just the last one, which is the tenth page, so it can show it to you on your screen. It is like a treasure hunt where the robot knows exactly which page you want to see.
Code Quality
Complexity Analysis
Solved On The Same Day
- problem 53: Maximum Subarray
- problem 56: Merge Intervals
- problem 78: Subsets
- problem 86: Partition List
- problem 89: Gray Code
- problem 90: Subsets II
- problem 98: Validate Binary Search Tree
- problem 179: Largest Number
- problem 456: 132 Pattern
- problem 473: Matchsticks to Square
- problem 542: 01 Matrix
- problem 916: Word Subsets
- problem 1930: Unique Length-3 Palindromic Subsequences
- problem 233: Number of Digit One
- problem 295: Find Median from Data Stream
- problem 315: Count of Smaller Numbers After Self
Extra
- Tech Stack - my development environment and tools
- AI - how AI helped me create this website (NOT solving the problems)
- Progress - progression over time
- Quality - code quality