palomo11
palomo11

Reputation: 23

Count number of occurrences of a number larger than x from every raw

I have a file with multiple rows and 26 columns. I want to count the number of occurrences of values that are higher than 0 (I guess is also valid different from 0) in each row (excluding the first two columns). The file looks like this:

X     Y     Sample1     Sample2     Sample3 ....     Sample24
a     a1         0        7           0                 0
b     a2         2        8           0                 0
c     a3         0        3           15                3 
d     d3         0        0           0                 0 

I would like to have an output file like this:

X     Y       Result
a     a1         1
b     b1         2
c     c1         3
d     d1         0

awk or sed would be good.

I saw a similar question but in that case the columns were summed and the desired output was different.

Upvotes: 0

Views: 163

Answers (3)

Ed Morton
Ed Morton

Reputation: 203995

$ awk '{print $1, $2, (NR>1 ? gsub(/ [1-9]/,"") : "Result")}' file
X Y Result
a a1 1
b a2 2
c a3 3
d d3 0

Upvotes: 0

karakfa
karakfa

Reputation: 67507

another awk

$ awk '{if(NR==1) c="Result"; 
        else for(i=3;i<=NF;i++) c+=($i>0); 
        print $1,$2,c; c=0}' file | column -t

X  Y   Result
a  a1  1
b  a2  2
c  a3  3
d  d3  0

Upvotes: 0

sjsam
sjsam

Reputation: 21965

awk 'NR==1{printf "X\tY\tResult%s",ORS} # Printing the header
      NR>1{
           count=0; # Initializing count for each row to zero
           for(i=3;i<=NF;i++){ #iterating from field 3 to end, NF is #fields
             if($i>0){ #$i expands to $3,$4 and so which are the fields
               count++; # Incrementing if the condition is true.
             }
           };
           printf "%s\t%s\t%s%s",$1,$2,count,ORS # For each row print o/p
          }' file

should do that

Upvotes: 1

Related Questions