Adding column values in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding column values in a file
# 1  
Old 01-10-2013
Adding column values in a file

Hi,

I am having a file in the following format.

Code:
for aaaa 
    1111 1234
    2222 3434

for bbbb
    1111 3434.343
    2222 2343
for cccc
    3333 2343.343
    4444 89000
for dddd
    1111 5678.343
    2222 890.3

aaaa 2343.343
bbbb 34343.343
cccc 778.0
dddd 78993.000


I have column 1 with following values aaaa,bbbb,cccc,dddd and each element aaaa,bbbb .. has some values assinged to them based on next column vlaues
1111,343.343 etc.

In the bottom of the file I have some more values in the following format.

Code:
aaaa 2343.343
bbbb 34343.343
cccc 778.0
dddd 78993.000

Now I want to add the values and the have the final o/p in the following format.
I want to grep first column aaaa,bbbb,cccc,dddd and get the sum of the last column values $NF and print them in the following format.
I want to add final column with the sum of values which are next to 1111,2222 and the final value which is next to aaaa in the bottom of the file.
for aaaa 7011.343(1234+3434+2343.343). Any help is greatly appreciated.

Code:
Hname frame capacity  Capacity(GB)
aaaa  1111   1234     7011.343(1234+3434+2343.343)
      2222   3434
bbbb  1111   3434.343  834343.
      2222   2343


Please let me know if I am not clear in explaning what I want. Thanks in advance.

Last edited by Scrutinizer; 01-10-2013 at 06:26 AM.. Reason: tags: quote -> code
# 2  
Old 01-10-2013
Quote:
Originally Posted by jpkumar10
Hi,

I am having a file in the following format.

Code:
for aaaa 
1111 1234
2222 3434

for bbbb
1111 3434.343
2222 2343
for cccc
3333 2343.343
4444 89000
for dddd
1111 5678.343
2222 890.3

aaaa 2343.343
bbbb 34343.343
cccc 778.0
dddd 78993.000

I have column 1 with following values aaaa,bbbb,cccc,dddd and each element aaaa,bbbb .. has some values assinged to them based on next column vlaues
1111,343.343 etc.

In the bottom of the file I have some more values in the following format.

Code:
aaaa 2343.343
bbbb 34343.343
cccc 778.0
dddd 78993.000

Now I want to add the values and the have the final o/p in the following format.
I want to grep first column aaaa,bbbb,cccc,dddd and get the sum of the last column values $NF and print them in the following format.
I want to add final column with the sum of values which are next to 1111,2222 and the final value which is next to aaaa in the bottom of the file.
for aaaa 7011.343(1234+3434+2343.343). Any help is greatly appreciated.

Code:
Hname frame capacity Capacity(GB)
aaaa 1111 1234 7011.343(1234+3434+2343.343)
2222 3434
bbbb 1111 3434.343 834343.
2222 2343

Please let me know if I am not clear in explaning what I want. Thanks in advance.
You are not clear at all.

Do you want the (1234+3434+2343.343) in the aaaa line output? If so, why isn't it also there on the bbbb line output.

On the bbbb line output, how do you get 834343. from 3434.343 + 2343 + 34343.343?

Why is there no output for the cccc nor dddd lines?

PS Please use CODE tags rather than QUOTE tags when posting sample input and output data.
# 3  
Old 01-10-2013
Sorry my bad. I was not clear.
1. I don't want (1234+3434+2343.343) to be displayed in my final o/p.
2. I think I had pasted wrong o/p for bbbb. The final tatal for bbbb would be 8120.343
3. I want the o/p for all the values which are next to for i.e for aaaa,bbbb,cccc,dddd...
I want the final o/p in the following format.

Code:
Hname frame capacity Capacity(GB)
aaaa  1111  1234      7011.343
      2222  3434
bbbb  1111  3434.343  8120.343
      2222  2343
cccc  3333  2343.343 92121.343
      4444  89000
dddd  1111  5678.343 85561.643
      2222  890.3

Thanks for quick reply. Your help is greatly appreciated.
# 4  
Old 01-10-2013
Hi jpkumar10,
I think I have something that works, but the spacing is different (making the computed 4th column values line up with the end of the heading for that column) and I disagree with the total for the bbbb line. (It looks to me like 3434.343 + 2343 + 34343.343 is 40120.686 rather than 8120.343.)
Code:
awk 'BEGIN {
        # Set headings and initial column widths.
        cw[1] = length(h[1] = "Hname") + 1
        cw[2] = length(h[2] = "frame") + 1
        cw[3] = length(h[3] = "capacity") + 1
        cw[4] = length(h[4] = "Capacity(GB)")
}
$1 == "for" {
        # Set the key for immediately following lines, the order in which this
        # key should appear in the output, the number of frame/capacity entries
        # found, and adjust the width of column 1 if we get a longer entry.
        key = $2
        fc[key] = s[key] = 0
        order[++forcnt] = key
        if(length(key) >= cw[1]) cw[1] = length(key) + 1
        next
}
$1 in s {
        # Update the sum for a key for an entry found in the final section of
        # the input.
        s[$1] += $2
        next
}
$1 == "" {
        # Skip empty lines.
        next
}
{       # Set the frame and capacity value for lines related to the current
        # key, update the count of entries found, update the sum for the
        # current key, and adjust the widths of columns 2 and 3 if we get
        # longer entries.
        frame[key, fc[key]] = $1
        cap[key, fc[key]++] = $2
        s[key] += $2
        if(length($1) >= cw[2]) cw[2] = length($1) + 1
        if(length($2) >= cw[3]) cw[3] = length($2) + 1
}
END {   # Check for a longer column 4 value.
        for(i = 1; i <= forcnt; i++)
                if(length(sprintf("%.3f", s[order[i]])) > cw[4])
                        cw[4] = length(sprintf("%.3f", s[order[i]]))
        # Print the headings.
        printf("%-*s%-*s%-*s%*s\n", cw[1], h[1], cw[2], h[2], cw[3], h[3],
                cw[4], h[4])
        # Loop through the keys in the order in which they were read.
        for(i = 1; i <= forcnt; i++) {
                # Print the key, 1st frame, 1st capacity, and sum for this key.
                printf("%-*s%-*s%-*s%*.3f\n",
                        cw[1], order[i], cw[2], frame[order[i], 0],
                        cw[3], cap[order[i], 0], cw[4], s[order[i]])
                # Print any remaining frame and capacity values for this key.
                for(j = 1; j < fc[order[i]]; j++)
                        printf("%-*s%-*s%s\n", cw[1], "",
                                cw[2], frame[order[i], j],
                                cap[order[i], j])

        }
}' input_file

As always, if you are using a Solaris system, use /usr/xpg4/bin/awk or nawk, instead of awk.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 01-10-2013
Thanks a lot. I will work on it now. This has been a great help.
# 6  
Old 01-11-2013
A simpler version without adaptive column widths:
Code:
awk '
  $1=="for"{
    A[I=$2]
    next
  } 
  NF{
    if($1 in A){
      sub(ORS, T[$1]+$2 ORS, A[$1])
      printf "%s",$1 A[$1]
    } 
    else {
      A[I]=sprintf("%s\t%s\t%-11s\n",A[I],$1,$2)
      T[I]+=$2
    }
  }
' infile

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove the values from a certain column without deleting the Column name in a .CSV file

(14 Replies)
Discussion started by: dhruuv369
14 Replies

2. Shell Programming and Scripting

Adding values of a column based on another column

Hello, I have a data such as this: ENSGALG00000000189 329 G A 4 2 0 ENSGALG00000000189 518 T C 5 1 0 ENSGALG00000000189 1104 G A 5 1 0 ENSGALG00000000187 3687 G T 5 1 0 ENSGALG00000000187 4533 A T 4 2 0 ENSGALG00000000233 5811 T C 4 2 0 ENSGALG00000000233 5998 C A 5 1 0 I want to... (3 Replies)
Discussion started by: Homa
3 Replies

3. Shell Programming and Scripting

Adding of two column values

Hi cat /tmp/xx.txt 1 4 1 5 1 6 2 1 2 1 2 1 i want to add the values of 2nd column resepect to 1st column values..for 1 in 1st column i need sum of all the values in 2nd column ..pls tell me hw to do it?? (8 Replies)
Discussion started by: Aditya.Gurgaon
8 Replies

4. Shell Programming and Scripting

Adding Column Values Using Pattern Match

Hi All, I have a file with data as below: A,FILE1_MYFILE_20130309_1038,80,25.60 B,FILE1_MYFILE_20130309_1038,24290,18543.38 C,FILE1_dsc_dlk_MYFILE_20130309_1038,3,10.10 A,FILE2_MYFILE_20130310_1039,85,110.10 B,FILE2_MYFILE_20130310_1039,10,12.10... (10 Replies)
Discussion started by: angshuman
10 Replies

5. UNIX for Dummies Questions & Answers

Adding column with values

Dear all, I need your help for my question please I have without header (space separated) and need to add two colomns at the beginning with values my file look like : rs1 a t 0.6 rs2 a c 0.3 rs3 t g 0.8 I need to a new file like: 1 100 rs1 a t 0.6 1 100 rs2 a c 0.3 1 100 rs3 t g... (3 Replies)
Discussion started by: biopsy
3 Replies

6. Shell Programming and Scripting

problem while adding column values in awk

Hi, I have a file "input.txt" with the following content : 5312,0,,,1,8,141.2,20090727 3714,0,,,1,8,285.87,20090727 5426,0,,,1,8,3.9,20090727 3871,0,,,1,8,30.4,20090727 9071,0,,,1,8,146.2,20090727 5141,0,,,1,8,2.8,20090727 0460,0,,,1,8,-0.1,20090727 7918,0,,,1,8,-0.1,20090727... (3 Replies)
Discussion started by: valokv
3 Replies

7. Shell Programming and Scripting

Adding the values of two file

I have two files as Count1 and Count2. The count contains only one values as 10 and count2 contains only one values as 20. Now I want third file Count3 as count1+Count2. That is it should contain sum of two file(10+20=30) (3 Replies)
Discussion started by: Shell_Learner
3 Replies

8. Shell Programming and Scripting

How to check Null values in a file column by column if columns are Not NULLs

Hi All, I have a table with 10 columns. Some columns(2nd,4th,5th,7th,8th and 10th) are Not Null columns. I'll get a tab-delimited file and want to check col by col and generate seperate error code for each col eg:102 if 2nd col value is NULL and 104 if 4th col value is NULL so on... I am a... (7 Replies)
Discussion started by: Mandab
7 Replies

9. Shell Programming and Scripting

Help in adding positive & negative values in a column

Hi Gurus, In my file I have an amount field from position 74 to 87, which contains values starting with '+' as well as '-'. I want to add all positive values in a varible called "CREDIT" and all negative values in a variable "DEBIT". I know, we can use grep to identify values with positive and... (4 Replies)
Discussion started by: berlin_germany
4 Replies
Login or Register to Ask a Question