awk getline Command parsing

Source: Internet
Author: User

The Getline command is one of the most powerful commands I personally think of awk. Because it completely changes the running logic of awk. Awk is essentially a for loop that processes each line of the input file and then executes the next line until each line of the entire file is executed. The whole process is automatic and you don't have to do anything. However, the Getline command allows you to control the loop. Of course, after the Getline command executes, awk sets these internal variables such as NF,NR,FNR and $ A.

Let's look at a simple example and print an even number from 1 to 10:

[email protected]:~/bash/awk$ seq 10 | awk ' {getline; print $} ' 246810
So what is the function of getline? As Getline's translation, get the line, but note that the resulting line is not the current row, but the next line of the current row. In the above example, awk first read to the first line, that is, 1, then getline, and then get 1 below the second line, is 2, because after Getline, awk will change the corresponding NF,NR,FNR and the internal variables such as $, so at this time the value of $ is no longer 1, It's 2, then print it out. And so on, you can get the results above. Similarly, we can use Getline to print only surprisingly few lines.
[email protected]:~/bash/awk$ seq 10 | awk ' {print $; getline} ' 13579
The only difference from printing even lines is that the order of print $ and getline is different. Because Getline is still the first line at this point after print $. Then getline,$0 becomes the next line of 2. The odd lines are printed out in turn.

The next thing we're going to change is that the odd-even line is swapped for printing, and the contents of the odd line are printed in even rows, and the contents of the even rows are printed in odd rows.

[email protected]:~/bash/awk$ seq 10 | awk ' {getline tmp; print tmp; print $} ' 21436587109
The above example puts the contents of the next line of getline in the TMP variable, so the internal variables such as NF,NR,FNR and $ are not changed.

In addition, Getline can also read the content from another file. The following example prints each line of two files on one line.

[Email protected]:~/bash/awk$ awk ' {printf '%s ', $ A; Getline < "B.txt"; Print $} ' A.txt 1 62 73 84 95 10
The contents of the A.txt file are the first column printed above, and the contents of the B.txt file are the second column printed above.

In addition, Getline can also be used to execute a UNIX command and get its output. The following example obtains the current time of the system through getline.

[Email protected]:~/bash/awk$ awk ' BEGIN {"date" | getline; Close ("date"); print $} ' Tue may 07:50:51 PDT 2016







awk getline Command parsing

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.