Home > front end >  Running math on a few columns in awk
Running math on a few columns in awk

Time:10-22

This seems like a simple problem but I find everything command line confusing.

I have a CSV with 5 columns. I want to multiply everything in columns 2-5 by a variable defined earlier in my bash script.

Obviously the below doesn't work, but show's what I'm trying to achieve:

awk -F , -v OFS=, 'seq($2 $5)*='$MULTIPLIER in.csv > out.csv

CodePudding user response:

Generally speaking:

awk -F, -v OFS=, -v m="${MULTIPLIER}" '{for (i=2;i<=5;i  ) $i*=m}1' in.csv > out.csv

Assuming there's a header record, and a variation on setting FS/OFS:

awk -v m="${MULTIPLIER}" 'BEGIN {FS=OFS=","} NR>1 {for (i=2;i<=5;i  ) $i*=m}1' in.csv > out.csv
  • Related