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

Using getline to get a variable from a file

Instead of reading both files' current input lines into $0, we can use the getline var format to read lines from a different file into a variable. In this method, none of the predefined variables are changed except the var variable. Its syntax is getline var < "filename". The following examples illustrate how this method works:

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

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

cars.dat($0)   :  1 maruti          swift       2007        50000       5emp.dat(tmp)   :  1 Jack    Singh   9857532312  jack@gmail.com      M   hr      2000cars.dat($0)   :  2 honda           city        2005        60000       3emp.dat(tmp) : 2 Jane ...

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