[tt]
BEGIN{ f = "[ \t]+[^ \t]+"; lastfour = f f f f "$" }
[/tt]
To make Awk execute some code before it starts reading the files listed
on the command line, enclose it in [tt]BEGIN{ ... }[/tt].
We want to build a regular expression that will match the last 4 fields.
To save typing, we use the variable [tt]f[/tt] to hold the pattern for
one field and its preceding whitespace. [tt][ \t][/tt] is a character
class that will match either a space or a tab. The following [tt]+[/tt]
means to match the preceding item at least once and as many times as
possible. The leading [tt]^[/tt] in [tt][^ \t][/tt] means match any
character except a space or a tab. Since [tt]lastfour[/tt] ends
with [tt]$[/tt], it will match only at the very end of the data string.
[tt]
sub( lastfour, "&&" )
[/tt]
Where the regular expression [tt]lastfour[/tt] is found, substitute
[tt]"&&"[/tt]. Since Awk wasn't given a variable upon which to perform
the substitution, it does it to [tt]$0[/tt], which is the record just
read. In Awk, an [tt]&[/tt] in the replacement string represents the
substring that was matched by the regular expression. Example:
[tt]
BEGIN {
$0 = "the cart or the horse"
gsub( /cart|horse/, "-&-" ) # gsub() does multiple substitutions.
print
}
the -cart- or the -horse-
[/tt]
If we want to replace with a literal [tt]-&-[/tt]:
[tt]
gsub( /cart|horse/, "-\\&-" )
[/tt]
Finally,
[tt]print[/tt]
prints [tt]$0[/tt] by default, since nothing was given it to print.