I have a list a variables being used in an awk command. They are passed parameters for a script so their value is undetermined but I am trying to use them in a for loop.
My question is can I use the value of a variable as part of another variables name? Or is this a bad idea and if so is there another route I can take?
Example
awk -v var1=hi -v var2=howdy -v var3=greetings \
'BEGIN{for (i = 1; i <= 3; ++i) print var+i}'
Desired Output
hi
howdy
greetings
+
does not concatenate strings in awk as it does in javascript and other languages. In awk, you simply write them next to one another (which has lower precedence than<
,>
, etc and higher than+
,-
, etc):a="foo"; b="bar"; print a b
=>foobar
. – Oct 30 '19 at 16:01ARGV
and replace them with an empty string; that will causeawk
to skip them when processing the command line args as file names:awk 'BEGIN{ for(i=1; i<=3; i++) { a[i] = ARGV[i]; ARGV[i] = "" } } {print}' hi howdy greetings /the/first/file/path
– Oct 30 '19 at 16:25