awk-the famous awk Oneliner, Part III: Selective output of specific lines

Source: Internet
Author: User
first 10 rows of the output file (analog head-n)

As mentioned earlier, the action is omitted here, which is the printout. The matching pattern is the variable nr needs less than 11,NR that is the current line number. The writing is simple, but there is a problem that when NR is greater than 10, awk actually makes a judgment on each line, and if the file is large, such as tens of thousands of lines, the wasted time is not negligible. So, the better way to do that is

The first sentence outputs the current row. The second sentence is whether it has arrived at line 10th and, if so, exits. first line of output file (analog Head-n 1)

This example is similar to the previous one, and the central idea is to exit the second line. the last two lines of the output file (analog Tail-n 2)

Indeed, it does seem a bit awkward. The first sentence always assigns a value to Y in front of the current line plus the variable x, and then records the current row with X. The effect is that the content of Y is always the previous line plus the contents of the current row. At the end, output the contents of Y. If you look carefully, it is not difficult to find this writing is very efficient, because it is constantly assigning and string concatenation, only to find the last line. So, if you want to output the last two lines of the file, Tail-n 2 is the best option. the last line of the output file (analog Tail-n 1)

There is nothing to say about syntax, and the print ellipsis argument is equivalent to print $. However, this statement may not be properly executed by some awk versions of non-GNU awk, and the following is the safest form of writing for compatibility:

output matches only certain patterns of rows (mock grep)

There seems to be nothing left to say. output does not match certain patterns of rows (analog grep-v)

Match mode before adding "!" is the negative judgment result. The previous line of the row that matches the pattern, not the current row

The variable x is always used to record the contents of the previous line, and if the pattern matches the current row, the content of X is output. The next line of output matching mode

This uses the Getline function to get the next line of content and output. The role of Getline is to place the $ $ content on the next line and update the NR,NF,FNR variable at the same time. If the match is the last line, Getline will be wrong, the $ $ will not be updated, and the last line will be printed. output matches a AA or BB or cc line

There's nothing to say, regular expressions. If you have friends who can not understand, please learn the regular expression. output lines that are longer than 65 characters

Length ([str]) returns the lengths of the string, which, if omitted, is a parameter of $, and parentheses can be omitted. output a line shorter than 65 characters

And the previous example is basically the same. output from matching rows to the last of the same content

This uses the form "PATTERN1,PATTERN2" to specify a matching range, where pattern2 is 0, or false, so it will always match to the end of the file. output from line 8th to line 12th

In the preceding example, this is also a range match. Output Line 52nd

If you want to perform less unnecessary loops, write this:

output two rows between regular expression matches
Delete all blank lines

NF is true is a non-empty line. Another way of writing is to use regular expressions:

Original post: http://www.cnblogs.com/mfryf/archive/2012/03/12/2391710.html

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.