In bash, I often run awk programs with many preset variables. Most of the times I use the same variable names in both bash and awk, so I call awk like the following:
awk -v var1="$var1" -v var2="$var2" -v var3="$var3" ....... ' # awk code '
This gets a little tedious when I have many variable names to type. Ideally, I would like to be able to run something like the following:
$(awkline var1 var2 var3 ......) ' # awk code '
where the output of the command "awkline" is the correct format of the awk command. For example, the "awkline" program could look like:
#!/bin/bash line="awk" for i in $*; do line="$line -v $i="'"$'"$i"'"'; done echo $line
If I then run "awkline a b c", I get what I expect:
awk -v a="$a" -v b="$b" -v c="$c"
However, if I try to run the command like $(awkline a b c), the value of the variable a inside awk becomes (literally including the quotes) "$a", and not the value of a, as I would like. How do I substitute all occurences of e.g. $a with the value of the variable a, so that I can run the awkline command?