I am trying to use one variable in my AWK (or GAWK) program to print multiple columns.
I am taking the columns to print from the command line:
gawk -v cols=1,2,3 -f sample.awk -F,
I want to be able to set this variable in my BEGIN{}
block, and use it in the main part of my program.
BEGIN{
split(cols, col_arr, FS)
i=1;
col_str = "$"col_arr[1];
for(col in col_arr){
if (i > 1){
col_str = col_str",$"col;
}
i++;
}
}
{
print col_str
}
However, this will just print "$1,$2,$3". How can I change this to print columns 1, 2, and 3?