The specified line of the [Linux] output file

Source: Internet
Author: User

1. Get the K line (take k=10 as an example)

Note that if the file contains less than 10 rows, it should not be output.

# Read from thefile file. txt and output the tenth line to stdout.# solution one, using awkawk 'NR = = Ten' file. txt# solution Two, using sed (personal test Result: Sed method faster than awk)sed-N'10p' filetxt# Solution III, combined use of tail and headTail-n+Ten file. txt |Head-n1# In addition, the following methods do not meet the requirementsHead file. txt-n10 |Tail-N1 # Because if the file contains less than 10 rows, the last line is output

Also, output line 5th to line 8th:

awk ' nr>=5 && NR <=8 ' file. txt

The topic comes from the 195 of Leetcode. Tenth line

Solution Reference: http://bookshadow.com/weblog/2015/03/28/leetcode-tenth-line/

2. Get some rows

sed ' /pattern/!p ' file. txt   //sed'5,9p'file. txt        // output V and nineth sed'ten, $p'file. TXT      // output line tenth to the last line

The specified line of the [Linux] output file

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.