awk summing specific lines and fields

Hi
I would like to know if it is possible to sum some specific fields.

I have this

 
x;x;x;x;x;x;x;x;467,390,611 Bytes;0.435291 GB;0.062247 GB;0.373045 GB;11,225;157
a;a;a;a;a;a;a;a;13,805,156,846 Bytes;12.857054 GB;1.838559 GB;11.018495 GB;151,063;18,933
b;b;b;b;b;b;b;b;232,797,478,723 Bytes;216.809547 GB;31.003765 GB;185.805782 GB;176,861;21,445;Parent: Line #545

I need to sum fields 9,10,11,12 except any line cotaining wording "Parent Line ...#

so currently I am doing something like grep -v -i "Parent" >new file,
then awk to print specific fields to new files,

and doing it through it.

 
awk "{s+=$1} END {print s}"

for this case desired output would be to sum only lines

 
x;x;x;x;x;x;x;x;467,390,611 Bytes;0.435291 GB;0.062247 GB;0.373045 GB;11,225;157
a;a;a;a;a;a;a;a;13,805,156,846 Bytes;12.857054 GB;1.838559 GB;11.018495 GB;151,063;18,933

Edit: adding more information:
I have a very long list nearly 1100 lines,
Each line is very similar to these

x;x;x;x;x;x;x;x;467,390,611 Bytes;0.435291 GB;0.062247 GB;0.373045 GB;11,225;157
a;a;a;a;a;a;a;a;13,805,156,846 Bytes;12.857054 GB;1.838559 GB;11.018495 GB;151,063;18,933
b;b;b;b;b;b;b;b;232,797,478,723 Bytes;216.809547 GB;31.003765 GB;185.805782 GB;176,861;21,445;Parent: Line #545

And I need to sum all fields to obtain: the total number of bytes, and GB. (while removing those lines that contain wording "Parent: Line"

For my example the total number of bytes and gigabytes, would be something similar to:

14272547457 Bytes;13.292345 GB;1.900806 GB;11.39154 GB

I get those numbers by summing all the row of fields $9, $10, $11, $12

467,390,611 Bytes + 13,805,156,846 Bytes = 14272547457 Bytes
0.435291 GB + 12.857054 GB = 13.292345 GB
0.062247 GB + 1.838559 GB = 1.900806 GB
0.373045 GB + 11.018495 GB = 11.39154 GB

nawk -F';' -v f='10;11;12;13;14' 'BEGIN{ n=split(f,a,FS)}!/Parent: Line/{for(i=1;i<=n;i++) s+=$a} END(for(i=1;i<=n;i++) print "sum of field " a ": " s}' myFile

@vgersh99
You need to add a conversion step (GB, MB, KB ...) as well as to remove superfluous comas since the dot stand for decimal separator.

awk -F";" 'tolower($0)!~/parent: line/{k=1024;m=k*k;g=k*m;for(i=10;i<=14;i++) {gsub(",",z,$i);split($i,a," ");s+=a[1]*(a[2]=="GB"?g:a[2]=="MB"?m:a[2]=="KB"?k:1)} print $0 FS "sum=" s/m "[MB]="s/g "[GB]"}' OFS=";" yourfile

Idented properly :

awk -F";" 'tolower($0)!~/parent: line/{
k=1024;m=k*k;g=k*m;
for(i=10;i<=14;i++) {
        gsub(",",z,$i);split($i,a," ")
        s+=a[1]*(a[2]=="GB"?g:a[2]=="MB"?m:a[2]=="KB"?k:1)
    }
print $0 FS "sum=" s/m "[MB]="s/g "[GB]"
}' OFS=";" yourfile

I have to apologize, my original post is not clear enough.
I have removed my original requirement to sum fields $13 and $14 because it is adding complexity and I don't really require them for the moment.
I will try to clarify my requirement,
I have a very long list nearly 1100 lines,
Each line is very similar to these

x;x;x;x;x;x;x;x;467,390,611 Bytes;0.435291 GB;0.062247 GB;0.373045 GB;11,225;157
a;a;a;a;a;a;a;a;13,805,156,846 Bytes;12.857054 GB;1.838559 GB;11.018495 GB;151,063;18,933
b;b;b;b;b;b;b;b;232,797,478,723 Bytes;216.809547 GB;31.003765 GB;185.805782 GB;176,861;21,445;Parent: Line #545

And I need to sum all fields to obtain: the total number of bytes, and GB. (while removing those lines that contain wording "Parent: Line"

For my example the total number of bytes and gigabytes, would be something similar to:

14272547457 Bytes;13.292345 GB;1.900806 GB;11.39154 GB

I get those numbers by summing all the row of fields $9, $10, $11, $12

467,390,611 Bytes + 13,805,156,846 Bytes = 14272547457 Bytes
0.435291 GB + 12.857054 GB = 13.292345 GB
0.062247 GB + 1.838559 GB = 1.900806 GB
0.373045 GB + 11.018495 GB = 11.39154 GB

Try this:-

sed 's/,//g;s/Bytes//g;s/GB//g;s/ //g' input_file | awk -F";" '
BEGIN {
        s1=0;
        s2=0;
        s3=0;
        s4=0;
} !/Parent/ {
        s1+=$9;
        s2+=$10;
        s3+=$11;
        s4+=$12;
}
END {
        printf("%.0f Bytes %f GB %f GB %f GB\n",s1, s2, s3, s4);
} '
1 Like

Worked perfectly, thanks