I have a file with around 27,000 columns and 40,000 rows. I need to count the number of 0.0s in each column and in each row of the data. I have written the following code to count and print the number of 0.0s within each column:

awk '{a[$1]++;} END{for(i in a) print a[i]"  "i}' file_name.txt

I tried to integrate the following code:

awk -F, '{ for(i=NF; i>=2; --i) printf "%s ", $i; print $1 }' file_name.txt

in order to create a loop where all the 0.0s were counted and printed. It is not necessary for me to create an output file for the final numbers.

  • Give a small example of what the output may look like. Should the output be organised in any particular way? – Kusalananda Oct 24 '18 at 7:38

Similar to my recent answer but here we need to check if the field value is match with 0.0 and count both column and rows into separate array instead of sum-up, so:

awk '{
    for (i=1; i<=NF; i++) {
        if ($i=="0.0") { zero_in_column[i]+= 1 ; zero_in_row[NR]+= 1 }; }
END { for (X in zero_in_row)
         print "in_row:"X, zero_in_row[X], "in_column:" X, zero_in_column[X]
}' OFS='\t' infile

Note: replace zero_in_row array with zero_in_column in END { for (X in zero_in_row) if number_of_column > number_of_rows.

for the input like below (and I had END { for (X in zero_in_column) since my column count is larger than rows count):

1    0.0  3    0.0  4    0.0  0.0
3    4    5    0.0  0.0  0.0  0.0
0.0  0.0  0.0  0.0  0.0  0.0  0.0

The output is:

in_row:1        4       in_column:1     1
in_row:2        4       in_column:2     2
in_row:3        7       in_column:3     1
in_row:4                in_column:4     3
in_row:5                in_column:5     2
in_row:6                in_column:6     3
in_row:7                in_column:7     3
  • The END block here assumes that there are as may columns as rows, or that there are at least as many rows as columns. – Kusalananda Oct 24 '18 at 7:37
  • Yeah, already explained in such a way and understandable in my answer, It can be validate before loop but no useful when you know your file column and rows, so I ignored – αғsнιη Oct 24 '18 at 7:54

Why not (small adaption to devWeek's proposal, untested):

awk '
        {ROWCNT = 0
         for (i=1; i<=NF; i++) if ($i == "0.0")  {COLCNT[i]++
         print "Row", NR,":", ROWCNT
END     {for (i=1; i in COLCNT; i++) print "Col", i,":", COLCNT[i]
' file

to keep the order of input lines, as the order in which array elements are retrieved is not defined.

  • that's broken; it will print the number of rows and number of cols irrespective of the content of the cells ;-) – mosvy Oct 24 '18 at 18:40
  • Rats - one "=" too few in the if - corrected. Thanks @mosvy – RudiC Oct 24 '18 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.