Visit Our UNIX and Linux User Community


awk summing specific lines and fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk summing specific lines and fields
# 1  
Old 11-01-2012
awk summing specific lines and fields

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

I have this
Code:
 
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.
Code:
 
awk "{s+=$1} END {print s}"

for this case desired output would be to sum only lines
Code:
 
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

Code:
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:
Code:
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

Last edited by nakaedu; 11-02-2012 at 02:02 AM..
# 2  
Old 11-01-2012
Code:
nawk -F';' -v f='10;11;12;13;14' 'BEGIN{ n=split(f,a,FS)}!/Parent: Line/{for(i=1;i<=n;i++) s[i]+=$a[i]} END(for(i=1;i<=n;i++) print "sum of field " a[i] ": " s[i]}' myFile

# 3  
Old 11-01-2012
@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.

Code:
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 :

Code:
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


Last edited by ctsgnb; 11-01-2012 at 09:15 PM..
# 4  
Old 11-02-2012
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

Code:
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:
Code:
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

Last edited by nakaedu; 11-02-2012 at 02:04 AM..
# 5  
Old 11-02-2012
Try this:-

Code:
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);
} '


Last edited by Yoda; 11-02-2012 at 02:33 AM..
This User Gave Thanks to Yoda For This Post:
# 6  
Old 11-02-2012
Quote:
Originally Posted by bipinajith
Try this:-

Code:
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);
} '


Worked perfectly, thanks

Previous Thread | Next Thread
Test Your Knowledge in Computers #949
Difficulty: Medium
In computer security, term virus and malware refer to the exact same thing.
True or False?

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep or awk a unique and specific word across many fields

Hi there, I have data with similar structure as this: CHR START-SNP END-SNP REF ALT PATIENT1 PATIENT2 PATIENT3 PATIENT4 chr1 69511 69511 A G homo hetero homo hetero chr2 69513 69513 T C . hetero homo hetero chr3 69814 69814 G C . . homo homo chr4 69815 69815 C A hetero . . hetero is... (10 Replies)
Discussion started by: daashti
10 Replies

2. Shell Programming and Scripting

awk to combine lines if fields match in lines

In the awk below, what I am attempting to do is check each line in the tab-delimeted input, which has ~20 lines in it, for a keyword SVTYPE=Fusion. If the keyword is found I am splitting $3 using the . (dot) and reading the portion before and after the dot in an array a. If it does have that... (12 Replies)
Discussion started by: cmccabe
12 Replies

3. Shell Programming and Scripting

Getting max value of specific fields with awk

Hello All, Here is am trying to get maximum value of third field depending on first,second and fourth fields with awk command . delimeter is pipe(|) . input 0221|09|14.25|aaa 0221|09|44.27|aaa 0221|09|44.33|aaa 0221|09|44.53|bbb 0221|09|34.32|bbb 0221|09|37.13|bbb... (5 Replies)
Discussion started by: sayami00
5 Replies

4. UNIX for Dummies Questions & Answers

Read the file and generate specific fields by awk

Hi I need to generate these output file from the below input file. Output : customer_id as customer, zip as zip_cd, catg_cd as catg, Input: out.customer::in.customer_id; out.zip_cd::in.zip; out.catg::in.catg_cd; Could you please help me on this. Please use code tags next... (1 Reply)
Discussion started by: Murugesh
1 Replies

5. Shell Programming and Scripting

Summing all fields in a file

I was playing around to see how stuff works, and was trying to sum all fields in a file. cat file 1 2 3 4 5 6 7 8 9 10 11 12 I made this script: awk 'BEGIN {OFS=RS}{$1=$1}{s+=$0} END {print "sum="s}' file This gives 15, why not 78? I test it like this awk 'BEGIN... (5 Replies)
Discussion started by: Jotne
5 Replies

6. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum

Hi friends, This is sed & awk type question. It is slightly different from my previous question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers (but no more than 10 numbers in series) whenever i find it and produce an output file with the... (4 Replies)
Discussion started by: kaaliakahn
4 Replies

7. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

8. UNIX for Dummies Questions & Answers

Updating specific fields with awk using conditions

Can someone help me again, I think with awk? I have a file with 4 columns (pipe-delimited): I just want to convert the last field so that e1 is now 'message 1', e2 is 'message 2', e0 is 'message 3', etc. I don't want to change any other columns because the e0-e10 code may appear as part of a... (4 Replies)
Discussion started by: giannicello
4 Replies

9. Shell Programming and Scripting

summing up the fields in fixed width file

Hi, I have a fixed width file with some records as given below: " 1000Nalsdjflj243324jljlj" "-0300Njfowjljl309933fsf" " 0010Njsfsjklj342344fsl" I want to sum-up first field values(i.e from 2nd character to 6th character)of each record. so for the above file i want to add (1000 - 300+... (2 Replies)
Discussion started by: srilaxmi
2 Replies

Featured Tech Videos