Multiply certain column to variable

Hi experts,
I want to multiply certain columns to variable,
data :

1 2 3 4 5 6 7 8 9

result with var = 2 for column 3,6,9 ... (every columns which can be divided to 3):

1 2 6 4 5 12 7 8 18

I have tried :

awk 'BEGIN{FS=OFS=" "}{print $1,$2,$3*a,$4,$5,$6*a,$7,$8,$9*2 }' a=2 file.txt

but how can I do it automatically (because I have like 1000 columns),
not manually by typing the columns no ? :confused:

Thanks for the help.

perl -e '$/=" ";open I,"< inputfile";$i=1;for(<I>){($i%3==0)?print $_*2 ." ":print $_;$i++};close I'

Is it in anyway related to the below post?

that post was another case,
would it be possible to find the solution in awk,
I am totally new-by in perl, .. :wall:

echo "1 2 3 4 5 6 7 9 12" | awk -v var=2 '{for(i=3;i<=NF;i=i+3) {$i= $i*var}  }1'
1 Like

Try this..

awk '{for(i=1;i<=NF;i++){printf (i%3?i:i*var)OFS}}' var=2 input_file

--ahamed

@tarun : Thanks, it works !!
@ahamed : it seems the command did not write the data, but the columns no.. thanks anyway

Thread hijack moved to new thread under programming: