O'Reilly logo

Learning AWK Programming by Shiwang Kalkhanda

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Getline from a file

In this method, we use the getline command to read the next line from a file other than the current input file. Here, the filename is called using the < "filename" redirection operator and is enclosed between double quotes. In this case, the values of NR and FNR are not changed. However, the value of NF is changed; its syntax is getline < "filename". The following examples illustrate how this method works:

$ vi getline_file.awk{    print "cars.dat : ",NR,$0;    getline < "emp.dat"    print "emp.dat  : ",NR,$0;}$ awk -f getline_file.awk cars.dat

The output of the execution of the previous code is as follows:

cars.dat :  1 maruti          swift       2007        50000       5emp.dat  :  1 Jack    Singh   9857532312  jack@gmail.com      M   hr      2000cars.dat : 2 honda city 2005 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required