How would you print just the 10th line of a file?
For example, assume file.txt
. Has the following content:
Line 1Line 2Line 3Line 4Line 5Line 6Line 7Line 8Line 9Line 10
Your script should output the tenth line, which is:
Line 10
[Show hint]
Hint:
1. If The file contains less than ten lines, what should do you output?
2. There ' s at least three different solutions. Try to explore all possibilities.
This topic can only use bash, previously did not pay much attention to Leetcode show hint, today accidentally opened to see, very enlightening.
The first thing I thought was:
Head-10 FILE.TXT|TAIL-1
After running five test case, something went wrong.
Input: 1\n2\n3\n4\n5\n6\n7\n8\n9
Output:9
Expected:
This is the above mentioned in hint, when he is less than 10 lines, should output what
So, instead of SED, pass the test,Runtime: Ms.
Sed-n ' 10p ' file.txt
Leetcode question 195th---tenth line