Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Running math on a few columns in awk

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading