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 into a variable

In this method, we fetch the next input line of the current file into a variable instead of storing it in $0. Its syntax is getline var. The following example illustrates how this method works:

$ vi getline_var.awk{    print "$0 ->    : ",NR, $0    getline tmp;    print "tmp ->    : ",NR, tmp;}$ awk -f getline_var.awk cars.dat

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

$0 ->    :  1 maruti          swift       2007        50000       5tmp ->   :  2 honda           city        2005        60000       3$0 ->    :  3 maruti          dezire      2009        3100        6tmp ->   :  4 chevy           beat        2005        33000       2$0 ->    :  5 honda           city        2010        33000       6tmp ->   :  6 chevy           tavera      1999        10000       4$0 ->    :  7 toyota          corolla     1995        95000       2tmp ->   :  8 maruti          swift       2009        4100        5$0 ->    :  9 maruti          esteem      1997        98000       1tmp -> : 10 ford ikon 1995 80000 ...

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