Sponsored Content
Top Forums Shell Programming and Scripting Adding column values in a file Post 302754249 by Don Cragun on Thursday 10th of January 2013 09:40:19 AM
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:
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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

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

7. 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

8. 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

9. 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
GRIB_COMPARE(1) 						   User Commands						   GRIB_COMPARE(1)

NAME
grib_compare - Compares the grib messages contained in two files. DESCRIPTION
Compares the grib messages contained in two files one by one in the same order. If some differences are found it fails returning an error code. All the keys are compared except those listed with the -b option and those that are specific of a different grib edition. Also data values are compared and are considered as different if their maximum absolute difference is greater than the absolute error, that by default is 0.000001. The value used by the absolute error can be set with te -e option. USAGE
grib_compare [options] grib_file grib_file OPTIONS
-b key,key,... Black list. All the keys in this list are skipped when comparing the two files. -c key,key,... Keys to compare. Only the listed keys are compared. -e tolerance Only values whose difference is more than tolerance are considered different. -w key[:{s/d/l}]{=/!=}value,key[:{s/d/l}]{=/!=}value,... Where clause. Grib messages are processed only if they match all the key/value constraints. A valid constraint is of type key=value or key!=value. For each key a string (key:s) or a double (key:d) or a long (key:l) type can be specified. Default type is string. -f Force. Force the execution not to fail on error. -V Version. -7 Does not fail when the message has wrong length -v Verbose. AUTHOR
This manpage has been autogenerated by Enrico Zini <enrico@debian.org>from the command line help of grib_compare. grib_compare April 2009 GRIB_COMPARE(1)
All times are GMT -4. The time now is 02:43 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy