2017-11-30 5 views
0

여러 행과 26 개의 열이있는 파일이 있습니다. 각 행 (첫 번째 두 열 제외)에서 0보다 큰 값의 발생 횟수를 계산하려고합니다. 이 파일은 다음과 같습니다모든 원시에서 x보다 큰 숫자의 발생 횟수를 계산합니다.

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 

내가 출력이 같은 파일을 가지고 싶다 :

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

AWK 또는 좋은 것 나오지.

비슷한 질문을 보았지만이 경우 열이 합산되고 원하는 출력이 다릅니다.

답변

0

다른 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 
1
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 

어떻게해야 그

0
$ 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 
관련 문제