awk variables are ususally referenced by name only.
The use of $ in front of a variable treats it like a pointer.
print NF - display the number of fields on a line.
print $NF - display contents of the last field on a line.
awk provides a number of useful predefined variables.
| variable | function | example | |||
| FILENAME | Contains the name of current data file being processed. Useful when user has specified multiple data files. | ||||
| FS | Field separator, identifies character that separates fields on line. Usually whitespace. |
FS=":" - treat each colon as a field separator. FS=":+" - treat multiple consecutive colons as a singe field separator. FS="[ \t]+" - treat multiple consecutive whitespace as a singe field separator. |
|||
| NF | Number of fields in current line. |
BEGIN { max=0; }
{
# remember the most fields used in the file
if ( max < NF )
{ max = NF }
}
END { print max; }
|
|||
| NR | Number of records read so far. If multiple data files provided, this will continue to accumulate for a grand total. | ||||
| FNR | File number of records. If working with multiple data files, this resets between data files. | ||||
| RS | Record separator - specifies record seperator, usuall new line, \n. Sometimes useful to specify alternatives based on data. |
An active web page returns : mytext=data&pass=password FS="="; RS="&"; |
|||
| OFS | Output field separator. Default separator when working with print |
|
|||
| ORS | Output record separator - works with print. Note that neither OFS or ORS work with printf. |
|
|||
| $0 | Represents the whole current record being processed. | ||||
| $1-$n | References the specified field on the current line. If referenced field doesn't exist, will be assigned null. |
||||
| Others | There are additional less commonly used predefined variables. |