Tanto o MAWK (versão padrão do AWK do Ubuntu) quanto o GAWK lêem argumentos de linha de comando que não são opcionais no formulário identifier=value
como atribuições de variáveis, que são executadas logo após as instruções BEGIN
terem sido executadas:
% echo | mawk -- '{print var}' var=foo
foo
% echo | gawk -- '{print var}' var=foo
foo
% echo foo bar baz | mawk -- '{$1=$1}1' OFS=,
foo,bar,baz
% echo foo bar baz | gawk -- '{$1=$1}1' OFS=,
foo,bar,baz
Eu prefiro este método para a opção -v
porque mantém o comando mais limpo, separando claramente as opções a serem passadas para o interpretador e os detalhes do programa.
De man mawk
:
The command line arguments divide into three sets: file arguments, assignment arguments and empty strings "". An assignment has the form var=string. When an ARGV[i] is examined as a possible file argument, if it is empty it is skipped; if it is an assignment argument, the assignment to var takes place and i skips to the next argument; else ARGV[i] is opened for input.
De man gawk
:
If a filename on the command line has the form var=val it is treated as a variable assignment. The variable var will be assigned the value val. (This happens after any BEGIN rule(s) have been run.) Command line variable assignment is most useful for dynamically assigning values to the variables AWK uses to control how input is broken into fields and records. It is also useful for controlling state if multiple passes are needed over a single data file.