{x}
blog image

Tenth Line

Given a text file file.txt, print just the 10th line of the file.

Example:

Assume that file.txt has the following content:

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10

Your script should output the tenth line, which is:

Line 10
Note:
1. If the file contains less than 10 lines, what should you output?
2. There's at least three different solutions. Try to explore all possibilities.

Solution Explanation for LeetCode 195: Tenth Line

This problem asks us to extract the tenth line from a file named file.txt. The challenge lies in handling cases where the file has fewer than ten lines.

Solution 1: Using sed

This solution utilizes the sed command-line utility, a powerful stream editor. sed is particularly efficient for this task because it operates directly on the stream of text from the file.

Code (Shell):

sed -n 10p file.txt

Explanation:

  • sed: This invokes the stream editor.
  • -n: This option suppresses the default behavior of sed, which is to print every line. We only want to print the 10th line.
  • 10p: This is the sed command. 10 specifies the line number (10th line), and p stands for "print". This command tells sed to print only the 10th line.
  • file.txt: This is the input file.

Time Complexity Analysis:

The time complexity of this solution is O(N), where N is the number of lines in the file. In the worst case, sed needs to read through all N lines to determine whether a 10th line exists. However, sed's efficient line-by-line processing makes it very fast even for large files. It doesn't need to load the whole file into memory at once.

Space Complexity Analysis:

The space complexity is O(1) (constant space). sed processes the file line by line, needing only a constant amount of memory to store the current line and perform the printing operation.

Handling Fewer Than 10 Lines:

If file.txt contains fewer than 10 lines, sed -n 10p file.txt will simply output nothing (an empty line). This implicitly handles the case where the 10th line doesn't exist. This behavior might be considered a feature; however, some users might desire explicit error handling, which could be done with additional shell scripting.

Alternative Solutions (Conceptual):

While sed provides a concise and efficient solution, other approaches are possible:

  • Using awk: awk is another powerful text processing tool that can achieve the same result. A similar approach to sed would be used, targeting the 10th line.

  • Using a scripting language (e.g., Python, Perl): These languages allow for more explicit error handling and flexibility. You could read the file line by line, counting lines, and printing the 10th line (or handling the case where the 10th line is not found). This would provide greater control and the ability to customize the output in cases of less than 10 lines.

The sed approach presented above is, however, the most efficient and elegant solution for this particular problem due to its speed and conciseness.