The principe
In awk, whenever you want to loop on a variable number of fields in a record (which usually represents a text line, the fields being words separated by spaces), you have to keep in mind that:
- the number of fields in the currently processed record is stored in the
variable
NF
; - the value of a field can be retrieved by prefixing the character
$
to an numerical variable: if the variablei
holds3
, then$i
is the same as$3
and represents the value of the third field in the current record.
Implementation
Sample data
To illustrate these functionalities of awk, we'll try to process a text file having a variable number of fields on each line. The first field of each line represents a name, followed by figures associated with the person.
Here is an example of such a file, that we'll designate as the file loto.txt
:
# Name list_of_figures
Philip 2 65 12 96
Turanga 55 34 82
Hubert 23
Amy 44 12 9 24
Hermes 32 99
Minimaliste program
In the following awk program that aims to process the above text file, the first line ignores text lines beginning by a hash mark (as it represents a comment). The following block, that applies to all other lines, prints on a new line each number associated with the person, which are the fields of the line starting with the second one (the first one being the name of the person):
/^/ next
for i=2; i<=NF; i++
printf "%s received the number %d\n", $1, $i
}
}
Runngin the program on the sample data
Our little awk program can be used from a shell interpreter, as shown in the example session below.
It will scan the file logo.txt
, ignoring its first line as it begins with a
hash mark, then, for each following line, it will print a new line on its
output for each figure associated with the person: