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
cdb(5)								File Formats Manual							    cdb(5)

NAME
cdb - Constant DataBase file format DESCRIPTION
A cdb database is a single file used to map `keys' to `values', having records of (key,value) pairs. File consists of 3 parts: toc (table of contents), data and index (hash tables). Toc has fixed length of 2048 bytes, containing 256 pointers to hash tables inside index sections. Every pointer consists of position of a hash table in bytes from the beginning of a file, and a size of a hash table in entries, both are 4-bytes (32 bits) unsigned integers in little-endian form. Hash table length may have zero length, meaning that corresponding hash table is empty. Right after toc section, data section follows without any alingment. It consists of series of records, each is a key length, value (data) length, key and value. Again, key and value length are 4-byte unsigned integers. Each next record follows previous without any special alignment. After data section, index (hash tables) section follows. It should be looked to in conjunction with toc section, where each of max 256 hash tables are defined. Index section consists of series of hash tables, with starting position and length defined in toc section. Every hash table is a sequence of records each holds two numbers: key's hash value and record position inside data section (bytes from the begin- ning of a file to first byte of key length starting data record). If record position is zero, then this is an empty hash table slot, pointed to nowhere. CDB hash function is hv = ((hv << 5) + hv) ^ c for every single c byte of a key, starting with hv = 5381. Toc section indexed by (hv % 256), i.e. hash value modulo 256 (number of entries in toc section). In order to find a record, one should: first, compute the hash value (hv) of a key. Second, look to hash table number hv modulo 256. If it is empty, then there is no such key exists. If it is not empty, then third, loop by slots inside that hash table, starting from slot with number hv divided by 256 modulo length of that table, or ((hv / 256) % htlen), searching for this hv in hash table. Stop search on empty slot (if record position is zero) or when all slots was probed (note cyclic search, jumping from end to beginning of a table). When hash value in question is found in hash table, look to key of corresponding record, comparing it with key in question. If them of the same length and equals to each other, then record is found, overwise, repeat with next hash table slot. Note that there may be several records with the same key. SEE ALSO
cdb(1), cdb(3). AUTHOR
The tinycdb package written by Michael Tokarev <mjt@corpit.ru>, based on ideas and shares file format with original cdb library by Dan Bernstein. LICENSE
Public domain. Apr, 2005 cdb(5)
All times are GMT -4. The time now is 10:30 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy