Summing columns in line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Summing columns in line
# 1  
Old 11-05-2012
Summing columns in line

I have a file with the following format

Code:
 
AAAAA 1.34B       0.76B       0.00B        0.00B       0.00B       0.00B      0.00B       0.00B       0.00B       0.00B       0.00B       0.00B       0.00B       0.00B     0.90B       0.00B       0.00B       0.46B       0.00B       0.03B       0.00B       0.00B     0.00B       0.00B       0.00B       0.00B       0.00B       0.00B       1.00B       1.00B       1.00B   
.....
BBBBB  2.01B      -9.99B      -9.99B      -9.99B      -9.99B      -9.99B    -9.99B      -9.99B      -9.99B      -9.99B      -9.99B      -9.99B      -9.99B    -9.99B      -9.99B      -9.99B      -9.99B      -9.99B      -9.99B      -9.99B    -9.99B      -9.99B      -9.99B      -9.99B      -9.99B      -9.99B      -9.99B     -9.99B      -9.99B      -9.99B      -9.99B
.....

Each section (AAAAA or BBBBB) is one line of data, making 32 columns of data in a single line. I need to sum up the 29th-31st columns in each line, preserving the 1st column, while ignoring those which contain -9.99, and ignoring "B".

Output
Code:
AAAAA 3.00
....

Is awk the best solution for this?
# 2  
Old 11-05-2012
What have you tried so far?
# 3  
Old 11-05-2012
I know how to sum all of a specified column and all of the entire line, but have yet to figure out how to select specific columns within a line.

Code:
awk '{s+=$29} END {print $1 s}'

Code:
awk '{for(i=t=0;i<NF;) t+=$++i; $0=t}1'

# 4  
Old 11-05-2012
Code:
awk ' BEGIN {
        OFS = " ";
        sum = 0;
}
{
        for (i=2; i<NF; i++)
        {
                num = $i
                gsub(/B/,"",num);
                if(i==29 || i==30 || i==31)
                {
                        if(num > 0)
                                sum = sum + num;
                }
        }
        printf "%s %f\n", $1, sum;
        sum = 0;
} END {

} ' input_file

# 5  
Old 11-06-2012
That works great. I notice there are some instances where I need to ignore a 'T' so that I am always summing the correct columns.

Code:
AAAAA -8.88B T     0.00B       0.00B       0.00B       0.06B       0.03B     0.08B       0.00B       0.11B       0.00B       0.01B       0.00B       0.00B       0.00B     0.00B      -8.88B T     0.16B       0.01B       0.00B       0.00B      -8.88B T    -8.88B T  0.11B       0.12B      -8.88B T    -8.88B T     0.00B       0.00B      0.00B   0.00B       0.00B

Is there a simple way to ignore the 'T' when counting the columns, or should i strip those out one step prior?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk split columns after matching on rows and summing the last column

input: chr1 1 2 3 chr1 1 2 4 chr1 2 4 5 chr2 3 6 9 chr2 3 6 10 Code: awk '{a+=$4}END{for (i in a) print i,a}' input Output: chr112 7 chr236 19 chr124 5 Desired output: chr1 1 2 7 chr2 3 6 19 chr1 2 4 5 (1 Reply)
Discussion started by: jacobs.smith
1 Replies

2. Shell Programming and Scripting

Summing columns over group of lines

I have an input file that looks like: ID1 V1 ID2 V2 P1 P2 P3 P4 ..... n no. of columns 1 1 1 1 1.0000 1.0000 1.0000 1.0000 1 1 1 2 0.9999 0.8888 0.7777 0.6666 1 2 1 1 0.8888 0.7777 0.6666 0.5555 1 2 1 2 0.7777 0.6666 0.5555 0.4444 2 1 1 1 0.6666 0.5555 0.4444 0.3333 2 1 1 2 0.5555 0.4444... (4 Replies)
Discussion started by: sdp
4 Replies

3. Shell Programming and Scripting

Please Help!!!! Awk for summing columns based on selected column value

a,b,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,bb,cc,dd,ee,ff,gg,hh,ii a thru ii are digits and strings.... The awk needed....if coloumn 9 == i (coloumn 9 is string ), output the sum of x's(coloumn 22 ) in all records and sum of y's (coloumn 23 ) in all records in a file (records.txt).... (6 Replies)
Discussion started by: BrownBob
6 Replies

4. Homework & Coursework Questions

HELP with Unix scripts in summing columns in a file

1. The problem statement, all variables and given/known data: Hi guys, i'm a new guy here, and it's my first time creating a unix script. can you guys help me out here? i'd really appreciate it. :( Here's my problem: This is the file i'm using, it has 6 columns, the first three columns are... (12 Replies)
Discussion started by: ramneim
12 Replies

5. Homework & Coursework Questions

HELP with Unix scripts in summing columns in a file.

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Hi guys, i'm a new guy here, and it's my first time creating a unix script. can you guys help me out here? i'd... (3 Replies)
Discussion started by: ramneim
3 Replies

6. Shell Programming and Scripting

Using awk to summing from a given line

My file is something like this : 03.097 03.094 03.093 03.095 03.091 04.089 06.093 07.225 08.196 06.097 06.094 05.096 04.086 I'd like to sum it from a given line to another one , e.g.: from line 10 until line 20 What s the awk way solving this ? (1 Reply)
Discussion started by: firelink
1 Replies

7. Shell Programming and Scripting

Summing data on N. row of each line

Hi friends, lets assume that I have lines of data as follows rico 12 bct 58 pot 65 vft 41 dek 45 kbt 13 her 35 ubr 14 . . . . . . . . . . . . I want to calculate the sum of the numbers at the 2. row of each... (6 Replies)
Discussion started by: rpf
6 Replies

8. Shell Programming and Scripting

Summing values in columns

Basically I have to process a text file which has been sorted this way: John 12 John 13 John 10 John 900 Peter 20 Peter 30 Peter 32 The first column is a name, and the second an arbitrary value, both delimited by a space. How can I sum them up such that it would become: John 935... (2 Replies)
Discussion started by: Dwee
2 Replies

9. Shell Programming and Scripting

Reading several files and summing their content line-by-line

Hey, I am gettin a bit crazy with my script. I have several input datas with the same name (5.ill) in different folders (daysim_01, daysim_02, etc.). The 4. column of each of the data has to be summed with each other and then hass to be written in one new file. So file1: 1 1 0 1 2 1 1 2 ... (7 Replies)
Discussion started by: ergy1983
7 Replies

10. Shell Programming and Scripting

Summing the columns of a file

Hi All, I have a file like - num.txt 12, 34, 65, line1 34, 65, 89, line2 43, 65, 77, line3 I want to do two things - 1. Add first three columns of each line and print the line with largest value. i.e. (12+34+65) for 1st line and so on. 2. Add middle column of each line i.e.... (3 Replies)
Discussion started by: asahlot
3 Replies
Login or Register to Ask a Question