Linux Shell Scripting Tutorial (LSST) v1.05r3 | ||
Chapter 7: awk Revisited | ||
|
Our next example talks more about predefined variable of awk. Create awk file as follows:
$cat > def_var |
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 Variable | Meaning |
FILENAME | Name of current input file |
RS | Input record separator character (Default is new line) |
OFS | Output field separator string (Blank is default) |
ORS | Output record separator string (Default is new line) |
NF | Number of input record |
NR | Number of fields in input record |
OFMT | Output format of number |
FS | Field separator character (Blank & tab is default) |
Getting Starting with awk | Doing arithmetic with awk |