awk를 사용하여 열 수가 다른 각 행의 평균을 계산합니다.

awk를 사용하여 열 수가 다른 각 행의 평균을 계산합니다.

awk계산 하는데 사용할 수 있나요?평균각 행(각 행에는 서로 다른 열이 있음) 다음과 같은 파일이 있습니다. 첫 번째 열은 이름입니다. 각 행의 평균을 계산하고 입력 파일의 마지막 열에 결과를 인쇄하고 싶습니다.

입력 파일( data1.csv):

EMPLOYEE1,0.395314,0.384513,,
EMPLOYEE2,5.4908,5.2921,,
EMPLOYEE3,0.0002323,0.00022945,0.00023238,0.00022931
EMPLOYEE4,0.00335516,0.00328432,0.00340309,0.00327163
EMPLOYEE5,1.4816,1.4367,1.4854,1.4353
EMPLOYEE6,7.89E-06,7.93E-06,7.95E-06,7.87E-06
EMPLOYEE7,3.724E-06,3.8745E-06,3.9428E-06,3.7227E-06
EMPLOYEE8,0.699498,0.688892,0.704256,0.683486
EMPLOYEE9,33.5195,31.9736,33.6779,31.742

원하는 출력:

EMPLOYEE1,0.395314,0.384513,,,0.3899135
EMPLOYEE2,5.4908,5.2921,,,5.39145
EMPLOYEE3,0.0002323,0.00022945,0.00023238,0.00022931,0.00023086
EMPLOYEE4,0.00335516,0.00328432,0.00340309,0.00327163,0.00332855
EMPLOYEE5,1.4816,1.4367,1.4854,1.4353,1.45975
EMPLOYEE6,7.89E-06,7.93E-06,7.95E-06,7.87E-06,7.91E-06
EMPLOYEE7,3.72E-06,3.87E-06,3.94E-06,3.72E-06,3.82E-06
EMPLOYEE8,0.699498,0.688892,0.704256,0.683486,0.694033
EMPLOYEE9,33.5195,31.9736,33.6779,31.742,32.7282

다음을 시도했지만 awk열이 최대 NF보다 작은 행의 평균을 계산하지 않습니다.

awk  -F',' '{ s = 0; for (i = 2; i <= NF; i++) s += $i; print $1, (NF > 1) ? s / (NF - 1) : 0; }'  data1.csv

그리고

 awk -F','  '{sum=0; for (i=2;i<=NF;i++)sum+=$i; print $0,sum/(NF-1)}'  data1.csv

하지만 내 코드는 NF 라인을 변경하지 않습니다. 각 행의 NF를 변경하여 원하는 출력을 얻을 수 있습니까?

답변1

한 가지 방법은 다음과 같습니다.

$ awk -F',' -v OFS=',' '{ 
        s=0; 
        numFields=0; 
        for(i=2; i<=NF;i++){ 
            if(length($i)){ 
                s+=$i; 
                numFields++
            } 
        } 
        print $0, (numFields ? s/numFields : 0)}' data1.csv 
EMPLOYEE1,0.395314,0.384513,,,0.389914
EMPLOYEE2,5.4908,5.2921,,,5.39145
EMPLOYEE3,0.0002323,0.00022945,0.00023238,0.00022931,0.00023086
EMPLOYEE4,0.00335516,0.00328432,0.00340309,0.00327163,0.00332855
EMPLOYEE5,1.4816,1.4367,1.4854,1.4353,1.45975
EMPLOYEE6,7.89E-06,7.93E-06,7.95E-06,7.87E-06,7.91e-06
EMPLOYEE7,3.724E-06,3.8745E-06,3.9428E-06,3.7227E-06,3.816e-06
EMPLOYEE8,0.699498,0.688892,0.704256,0.683486,0.694033
EMPLOYEE9,33.5195,31.9736,33.6779,31.742,32.7282

0.389914awk에 의해 인쇄된 결과는 첫 0.779827/2번째 행의 평균이 0.389914가 아니라 가 됨을 의미합니다 0.389915. 이는 awk가 가장 가까운 짝수로 반올림되고 기본 인쇄 모드(변수에 의해 제어됨 OFMT)가 이기 때문입니다 %0.6g. 더 많은 정확성이 필요한 경우 다음을 수행할 수 있습니다.

$ awk -F',' -v OFS=',' -v OFMT='%0.7g' '{ 
        s=0; 
        numFields=0; 
        for(i=2; i<=NF;i++){ 
            if(length($i)){ 
                s+=$i; 
                numFields++
            } 
        } 
        print $0, (numFields ? s/numFields : 0)}' data1.csv 
EMPLOYEE1,0.395314,0.384513,,,0.3899135
EMPLOYEE2,5.4908,5.2921,,,5.39145
EMPLOYEE3,0.0002323,0.00022945,0.00023238,0.00022931,0.00023086
EMPLOYEE4,0.00335516,0.00328432,0.00340309,0.00327163,0.00332855
EMPLOYEE5,1.4816,1.4367,1.4854,1.4353,1.45975
EMPLOYEE6,7.89E-06,7.93E-06,7.95E-06,7.87E-06,7.91e-06
EMPLOYEE7,3.724E-06,3.8745E-06,3.9428E-06,3.7227E-06,3.816e-06
EMPLOYEE8,0.699498,0.688892,0.704256,0.683486,0.694033
EMPLOYEE9,33.5195,31.9736,33.6779,31.742,32.72825

관련 정보