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
CheckDigits::M10_001(3pm)				User Contributed Perl Documentation				 CheckDigits::M10_001(3pm)

NAME
CheckDigits::M10_001 - compute check digits for Bahncard (DE), IMEI, IMEISV, ISIN, Miles&More, Payback (DE), Personnummer (SE), Passport (BR), Credit Cards, SSN (US), Samordningsnummer (SE), VAT RN (ES), VAT RN (IT), VAT RN (SE), International Securities Identifikation Number (ISIN) SYNOPSIS
use Algorithm::CheckDigits; $visa = CheckDigits('visa'); if ($visa->is_valid('4111 1111 1111 1111')) { # do something } $cn = $visa->complete('4111 1111 1111 111'); # $cn = '4111 1111 1111 1111' $cd = $visa->checkdigit('4111 1111 1111 1111'); # $cd = '7' $bn = $visa->basenumber('4111 1111 1111 1111'); # $bn = '4111 1111 1111 111' DESCRIPTION
ALGORITHM 1. Beginning right all numbers are weighted alternatively 1 and 2 (that is the check digit is weighted 1). 2. The total of the digits of all products is computed. 3. The sum of step 3 ist taken modulo 10. 4. The check digit is the difference between 10 and the number from step 3. To validate the total of the digits of all numbers inclusive check digit taken modulo 10 must be 0. METHODS is_valid($number) Returns true only if $number consists solely of numbers and the last digit is a valid check digit according to the algorithm given above. Returns false otherwise, complete($number) The check digit for $number is computed and concatenated to the end of $number. Returns the complete number with check digit or '' if $number does not consist solely of digits and spaces. basenumber($number) Returns the basenumber of $number if $number has a valid check digit. Return '' otherwise. checkdigit($number) Returns the checkdigit of $number if $number has a valid check digit. Return '' otherwise. EXPORT None by default. AUTHOR
Mathias Weidner, <mathias@weidner.in-bad-schmiedeberg.de> SEE ALSO
perl, CheckDigits, www.pruefziffernberechnung.de. For IMEI, IMEISV: ETSI Technical Specification TS 100 508 (v6.2.0) perl v5.10.0 2008-05-17 CheckDigits::M10_001(3pm)
All times are GMT -4. The time now is 05:39 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy