[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Gawk manual correction. Option -v is more complex
From: |
Youni Git |
Subject: |
Gawk manual correction. Option -v is more complex |
Date: |
Thu, 08 Dec 2022 17:47:23 +0000 |
User-agent: |
Zoho Mail |
Hello, dear GNU! I have a request to improve the manual of Gnu awk gawk. In
last version of October, 2022, there is an explanation of command line option
"-v":-v var=val --assign var=val Set the variable var to the value val before
execution of the program begins.Such variable values are available inside the
BEGIN rule (see Section 2.3 [Other Command-Line Arguments], page 38).The -v
option can only set one variable, but it can be used more than once, setting
another variable each time, like this: ‘awk -v foo=1 -v bar=2 ...’.CAUTION:
Using -v to set the values of the built-in variables may lead to surprising
results. awk will reset the values of those variables as it needs to, possibly
ignoring any initial value you may have given.I tested, and got extremely
another result, that must be described in the manual.1. There is a difference,
where exactly in command line option -v appears. If program text or file
appears before definition of variable, this variable cannot be used in BEGIN
rule. Awk reads the command line like a code and runs it as a sequence of
commands and data. This is fundamental.2. Variable can vary for every file
used, when another value is placed before another file. Awk reads and set the
new value of variable if provided before processing of file defined after new
variable.Example:$ echo text1 > file1$ echo text2 > file2$ echo 'BEGIN {print
"at the beginning myvar is " myvar}; {print "myvar is " myvar, ", text is "
$0}; END {print "in the end myvar is " myvar};' > program.awk$ awk -f
program.awk myvar=kuku file1 myvar=auau file2at the beginning myvar ismyvar is
kuku , text is text1myvar is auau , text is text2in the end myvar is auauAwk
reads, uses and process the command line arguments in a sequence as like it is
a code. Awk runs the program file program.awk before variable myvar is set,
because actually variable is provided after program file is provided in command
line. Myvar is not set before running the code in BEGIN rule.And myvar is
changing after file1 was processed. In section END myvar has its last value.I
hope such example will be useful and will save time of programmers reading
manual and writing first awk programs with variables. I think it is fundamental
feature to set variable before processing of each file, and it must be
considered in the manual. $ awk -VGNU Awk 5.1.0, API: 3.0 (GNU MPFR 4.1.0, GNU
MP 6.2.0)Copyright (C) 1989, 1991-2020 Free Software Foundation.Best
regards.Youni.
- Gawk manual correction. Option -v is more complex,
Youni Git <=