Awk--getline

Source: Internet
Author: User

A Getline, on the whole, should understand its usage as such:

when the left and right no redirect | or <, getline action on the current file, read the first line of the current file to its heel of the variable var or $ (no variable); It should be noted that because Awk has read a line before processing getline, Getline gets The return result of the 1> is interlaced.

When there is a redirect | or < on its left and right, getline acts on the directed input file, because the file is just opened, and is not read into a line by awk, but Getline read in, then Getline returns the first line of the file, not interlaced. The following code shows both of these usages:

awk 'begin{nfile=4
Getline for(i=2; i<=nfile; i++) { file="Dosp"I getline<file }}{ for(j=1; j<=nf; J + +)sum[j]=$j for(i=2; i<=nfile; i++) { file="Dosp"I getline<file for(j=2; j<=nf; J + +)sum[j]+=$j} for(j=1; j<=nf; J + +) printf"%12.8f",sum[j] Print" " }'DOSP1


B. Getline usage can be broadly divided into three broad categories (each of which is divided into two small classes), that is, a total of 6 usages. The code is as follows:

Nawk ' begin{cat data.txt ' |  Getline D; Print d} ' data2.txtnawk ' begin{"cat data.txt" |getline; print $0} ' data2.txt Nawk ' Begin{getline D < ' data.txt '; print d} ' data2.txtnawk ' Begin{getline < ' data.txt '; print $0 } ' Data2.txt


The above four lines of code all implement "print only the first line of the Data.txt file" (If all lines are printed, loop) eg.
        Nawk ' begin{fs= ': ";  while (getline< "/etc/passwd" >0) {print $1}} ' data.txt        Nawk ' {getline D; print d ' # ' $3} ' Data.txt

Awk first reads in the first line, then processes the Getline function, then assigns the next line to the variable d, then prints D, because D is followed by a newline character, so the following # will overwrite D, and the subsequent one will also overwrite D.

Nawk ' {getline; print $0"#" $3} ' Data.txt

Awk first reads the first line and then processes the Getline function, then assigns the next line to $ A, and now the $ is is the next line, and the following # and $ (fetch from $) will overwrite the content of $ A.

To simply read a record

Awk's getline statement is used to simply read a record. Getline is especially useful if a user has a data record similar to two physical records. It completes the separation of the General field (SET field variable FNR NF NR). Returns 1 if successful, and 0 (reaches the end of the file) for failure. If you need to simply read a file, you can write the following code:

{while (getline==1) {#process the inputted fields}}

You can also enable Getline to save input data in a field instead of processing a generic field by using the Getline variable form. When this method is used, NF is placed into 0,FNR and NR is added.
Users can also use the getline< "filename" method to enter data from a given file instead of entering data from the command line. At this point, Getline will complete the General field separation (set the field variable to + and NF). If the file does not exist, returns 1, succeeds, returns 1, and returns 0 indicating failure. The user can read data from a given file into a variable, or it can be substituted with the stdin (standard input device) or a variable containing the filename. It is important to note that FNR and NR are not modified when using this method.

Another way to use the Getline statement is to accept input from a UNIX command, such as the following example:

{while ("who-u"|  Getline) {#process the inputted fields}}

Of course, you can also use the following form:

" Command " | Getline variable

Reference URL:

[1] awk's getline function usage:http://club.topsage.com/thread-357784-1-1.html

[2] Linux system different file corresponding column add: http://emuch.net/bbs/viewthread.php?tid=6357638

[3] Linux Implementation of two file content add: http://www.linuxidc.com/Linux/2007-07/5940.htm

[4] awk in Next and getline usage example: http://blog.csdn.net/imzoer/article/details/8738501

[5] awk getline () function application: http://blog.chinaunix.net/uid-10540984-id-356795.html

Awk--getline

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.