[Leetcode] Tenth line Tenth

Source: Internet
Author: User

How would you print just the 10th line of a file?

For example, assume. has the file.txt 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 problem lets us use bash script to print a TXT file line tenth, can be implemented in a number of ways, we first look at a method that is implemented with AWK, awk is a powerful text analysis tool with flow control, mathematical operations, Process Control, built-in variables and functions, loops and judgments of the function, Refer to this post for details. where NR represents the number of rows, and $ A is the current record, so we can use the if to determine the number of rows, the content will be printed out:

Solution One:

awk ' {if (NR = =) Print $} ' file. txt

We can also use a more concise wording to print out the tenth line as follows:

Solution Two:

awk ' NR = = Ten ' file. txt

We can also use the Stream editing tool sed to do this, for an explanation of SED can be found in this post. The-n default means that all lines are printed, and p limits the number of lines that are printed specifically:

Solution Three:

sed file. txt

We can also use the tail and head keywords to print, see this post for an explanation of tail and head usage. Where head means printing from the beginning, tail means printing from the end,-you mean printing according to the number of lines of the file, some differences and links are shown in the following examples:

Tail-n 3 file.txt: Print the last three lines of file

Tail-n +3 file.txt: Print everything from the third line of file

Head-n 3 file.txt: Print the first three lines of the file

Head-n-3 file.txt: Prints all the contents of the file in addition to the last three lines

As for the vertical bar | For piping commands, see this post, Usage: Command 1 | Command 2 His function is to pass the result of the first command Command1 execution as input to command 2 to command 2. Understanding this knowledge, then the following line of command is good to understand, first enter all the contents of the file from line tenth, and then print out the first line of the output is the line:

Solution Four:

Tail -n+filehead1

The following method is exactly the opposite of the above, output the first 10 rows of the file, and then start printing from the tenth line of the output, you can also print exactly the tenth line of content:

Solution Five:

Head Ten file Tail -n +

Resources:

Https://leetcode.com/discuss/29526/super-simple-solution

Https://leetcode.com/discuss/29591/simple-solution-using-awk

Leetcode all in one topic summary (continuous update ...)

[Leetcode] Tenth line Tenth

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.