awk -v input="$constrained" '
BEGIN {
n = split(toupper(input), user)
fmt = "%-4s %-8s NNN 0.050 0.050 0.050\n"
}
{print}
/<< matched line >>/ {
for (i=1; i<=n; i++)
printf fmt, user[i], user[i]"_GPS"
exit
}
' ./text_data > text_data_2
In the above codes, I want to define variable for 0.050 0.050 0.050
. I tried using with
echo -n "##### numeric value? ####?"
read value
fmt = "%-4s %-8s NNN $value $value $value\n"
but within the quotation marks, $value
variable cannot be defined. How can I define this variable inside the quotation marks?
You should be able to pass the variable using
-v
the same as you do for yourinput
variablethen use
sprintf
within yourBEGIN
block to construct the format string e.g.Note the use of
%%
to produce literal%
in thefmt
string.To test that it's constructing the correct format string, we can add print statements, first for the
fmt
string itself and then for the resulting print statement: