195. Tenth Line

Tenth Line

Tenth Line

Problem Description

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 are at least three different solutions. Try to explore all possibilities.


Solution

tail -n+10 file.txt | head -n1
Idea:

  • tail command used to print the last lines
  • the head command used to print top lines
  • -n is a flag (nth line)

Conclusion

That’s all folks! In this post, we solved LeetCode problem #195

I hope you have enjoyed this post. Feel free to share your thoughts on this.

You can find the complete source code on my GitHub repository. If you like what you learn. feel free to fork 🔪 and star ⭐ it.


In this blog, I have tried to solve leetcode questions & present the most important points to consider when improving Data structure and logic,  feel free to add, edit, comment, or ask. For more information please reach me here
Happy coding!

Comments

Popular Post