Na verdade, é em POSIX awk
(link para POSIX 2008, versões anteriores) eu também acredito). -v
é descrito na seção Opções , o outro caminho está na seção Operandos .
Há uma diferença entre -v
e passar as atribuições no final com os nomes dos arquivos:
- com
-v
:
The application shall ensure that the assignment argument is in the same form as an assignment operand. The specified variable assignment shall occur prior to executing the awk program, including the actions associated with BEGIN patterns (if any). Multiple occurrences of this option can be specified.
- Misturado com os nomes dos arquivos:
[...] Each such variable assignment shall occur just prior to the processing of the following file, if any. Thus, an assignment before the first file argument shall be executed after the BEGIN actions (if any), while an assignment after the last file argument shall occur before the END actions (if any). If there are no file arguments, assignments shall be executed before processing the standard input.
Exemplo:
$ cat input
hello
hello
$ awk -v var=one 'BEGIN{print var} /hello/{print var} END{print var}' \
var=two input var=three input var=four
one
two
two
three
three
four