Question :
I’m breaking my teeths under this:
awk -v full=wc -v empty=wp '{
...blablabla...
if ($4==full) stop=yes
if (stop==yes && $4==empty) exit
...blablabla...
}'
The code works well (I mean, I get an output) if I don’t declare the two variables full and empty at the beginning, and use instead the values of these in the script. If I only use the first variable in the script, I get the same output. But if I only use the second variable, I get no output at all.
Consider what happens when the variables are expanded. A portion of the awk body goes from:
to
Since
wcis a “bare word”, awk thinks it is a variable and substitutes it’s value (empty string), so you get this:When you’re building your awk script, you need to be aware of quoting strings in awk. You need:
However, it is much more elegant to pass values with awk’s
-voption as you have done.