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

Bash iterate through fields of a TSV file and divide it by the sum of the column

I have a tsv file with several columns, and I would like to iterate through each field, and divide it by the sum of that column:

Input:

A    1    2    1
B    1    0    3

Output:

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

A    0.5    1    0.25
B    0.5    0    0.75

I have the following to iterate through the fields, but I am not sure how I can find the sum of the column that the field is located in:

awk -v FS='\t' -v OFS='\t' '{for(i=2;i<=NF;i++){$i=$i/SUM_OF_COLUMN}} 1' input.tsv

>Solution :

You may use this 2-pass awk:

awk '
BEGIN {FS=OFS="\t"}
NR == FNR {
   for (i=2; i<=NF; ++i)
      sum[i] += $i
   next
}
{
   for (i=2; i<=NF; ++i)
      $i = (sum[i] ? $i/sum[i] : 0)
}
1' file file

A       0.5     1       0.25
B       0.5     0       0.75
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