0
BEGIN{FS=","; t1 = 0; n = 0;  avg = 0; sum=0;  printf("%-10s %9s\n%-10s %9s","Name", "Average", "----", "-------") }

{
    if ($3 > 0){
        t1 += $3;
        n++;
    }
    sum = 0;
    if($3 > 0 && $4 > 0 && $5 >0){
        sum = $3 + $4 + $5;
        avg = sum / 3;
    }
    else if($3 < 0 && $4 > 0 && $5 >0){
            sum = $4 + $5;
        avg = sum / 2;
    }
    else if($3 > 0 && $4 < 0 && $5 >0){
        sum = $3 + $5;
        avg = sum / 2;
    }
    else if($3 < 0 && $4 > 0 && $5 < 0){
        sum = $3 + $4;
        avg = sum / 2;
    }

    printf("\n%-10s %7.2f", $1, avg);
}
END{
    printf("-------------------\nAverage for Test ! : %f.2", (t1/n));

    }

body part command should ignore the first record.

  • 1
    For me it's not clear what is the problem. Please add input file (or at least part of it) and the actual and expected output? If you just want to skip the first record and the script is not part of the issue, why do you add it in the first place instead of a minimal example? – pLumo Apr 16 at 13:07
5

You can do this by adding a test on the record number to your block:

NR > 1 {
    if ($3 > 0){
        t1 += $3;
        n++;
    }
    ...

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.