Linux Shell Scripting Tutorial (LSST) v1.05r3
Prev
Chapter 7: awk Revisited
Next

Predefined variable of awk

Our next example talks more about predefined variable of awk. Create awk file as follows:

$cat > def_var
{
print "Printing Rec. #" NR "(" $0 "),And # of field for this record is " NF
}

Run it as follows.
$awk -f def_var inven
Printing Rec. #1(1. Pen 5 20.00),And # of field for this record is 4
Printing Rec. #2(2. Pencil 10 2.00),And # of field for this record is 4
Printing Rec. #3(3. Rubber 3 3.50),And # of field for this record is 4
Printing Rec. #4(4. Cock 2 45.50),And # of field for this record is 4

NR and NF are predefined variables of awk which means Number of input Record, Number of Fields in input record respectively. In above example NR is changed as our input record changes, and NF is constant as there are only 4 field per record. Following table shows list of such built in awk variables.

awk VariableMeaning
FILENAMEName of current input file
RSInput record separator character (Default is new line)
OFSOutput field separator string (Blank is default)
ORSOutput record separator string (Default is new line)
NFNumber of input record
NRNumber of fields in input record
OFMTOutput format of number
FSField separator character (Blank & tab is default)


Prev
Home
Next
Getting Starting with awk
Up
Doing arithmetic with awk