Sponsored Content
Full Discussion: awk and count sum ?
Top Forums Shell Programming and Scripting awk and count sum ? Post 302639471 by agama on Friday 11th of May 2012 10:44:23 PM
Old 05-11-2012
Quote:
Originally Posted by Corona688
But why are some subtracted, and some not?

I believe that "unknown" isn't in the input file, but is to be assumed to be the difference between the expected total (a week's worth of seconds) and the sum. So, after summing the location-type combinations, if that total isn't greater than a week's seconds, an unknown type is added to the output which is the difference.

@sabercats: I'm still not clear about how to handle sums that are larger than a week's worth of seconds. It seemed odd just to cap it. The code below caps location/type at a week's worth of seconds, and prints the unknown line only when the cap isn't reached. You can tweek it to always put out an unknown line if needed.

Code:
awk  -F , '
    {
        loc[$1];
        if( !seen[$1,$2]++ )
            type[$1] = type[$1] $2 ",";
        tsum[$1,$2] += $3+0;
    }

    END {
        wsec = 7 * 86400;
        for( l in loc )
        {
            sum = 0;
            sub( ",$", "", type[l] );
            split( type[l], t, "," );
            for( i = 1; i <= length( t ); i++ )
            {
                v =  tsum[l,t[i]] < wsec ? tsum[l,t[i]] : wsec;
                printf( "%s,%s,%d,%.2f%%\n", l, t[i], v, 100 * (v/wsec) );
                sum += v;
            }
            if( (diff = wsec - sum) > 0 )
                printf( "%s,%s,%d,%.2f%%\n", l, "unknown", diff, 100 * (diff/wsec) );
        }
    }
' input-file >output-file

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

count of sum of row60 files same patter

i have 60 files that have same pattern tgt_abc1.dat tgt_abc2.dat i want to calculate sum of row count of these files and sum these up and populate that in third file. how can i do that?? example tgt_abc1.dat 2000 tgt_abc2.dat 4000 so want to populate in file xyz.dat 6000 (1 Reply)
Discussion started by: er_zeeshan05
1 Replies

2. Shell Programming and Scripting

Getting Sum, Count and Distinct Count of a file

Hi all this is a UNIX question. I have a large flat file with millions of records. col1|col2|col3 1|a|b 2|c|d 3|e|f 3|g|h footer**** I am supposed to calculate the sum of col1 1+2+3+3=9, count of col1 1,2,3,3=4, and distinct count of col1 1,2,3=c3 I would like it if you avoid... (4 Replies)
Discussion started by: singhabhijit
4 Replies

3. UNIX for Dummies Questions & Answers

how to count number of rows and sum of column using awk

Hi All, I have the following input which i want to process using AWK. Rows,NC,amount 1,1202,0.192387 2,1201,0.111111 3,1201,0.123456 i want the following output count of rows = 3 ,sum of amount = 0.426954 Many thanks (2 Replies)
Discussion started by: pistachio
2 Replies

4. Shell Programming and Scripting

awk count characters, sum, and divide by another column

Hi All, I am another biologist attempting to parse a large txt file containing several million lines like: tucosnp 56762 T Y 228 228 60 23 .CcCcc,,..c.c,cc,,.C... What I need to do is get the frequency of periods (.) plus commas (,) in column 9, and populate this number into another... (1 Reply)
Discussion started by: peromhc
1 Replies

5. Shell Programming and Scripting

sum divided by count

Dear friends, I'm stuck with the task below, I would be thankful for all your replies. INPUT : Date Price Volume 20110601 73052811.61 2845833 20110602 61489062.96 9909230 20110603 72790724.65 1108927 20110606 48299507.20 7435881 20110607 ... (5 Replies)
Discussion started by: hernand
5 Replies

6. Shell Programming and Scripting

Count char, sum and change

Hello, I have some problem in counting char of word, sum and change. I'm not sure shell script can do this. Input data: Sam1 BB BB AA AA BB BB BB Sam2 BB BB AA AA AB AB AB Sam3 BB BB BB AA BB BB BB Sam4 AB AB AB AB AB AB AA Sam5 BB BB AA AA BB BB -- If I count in column 2, B is 9... (3 Replies)
Discussion started by: awil
3 Replies

7. Shell Programming and Scripting

Why sum of recs in awk don't match total rec count?

I'm using awk to determine if a field starting in position 604 for length of 10 is not equal to ALL spaces. It's searching several files which are in the current directory. The below awk indicates that there are 84 records on all files where this field IS NOT equal to ALL spaces ( there are 10... (2 Replies)
Discussion started by: mjf
2 Replies

8. Shell Programming and Scripting

Script Shell: Count The sum of numbers in a file

Hi all; Here is my file: V1.3=4 V1.4=5 V1.1=3 V1.2=6 V1.3=6 Please, can you help me to write a script shell that counts the sum of values in my file (4+5+3+6+6) ? Thank you so much for help. Kind regards. (3 Replies)
Discussion started by: chercheur111
3 Replies

9. Shell Programming and Scripting

Shell script count lines and sum numbers from multiple files

I want to count the number of lines, I need this result be a number, and sum the last numeric column, I had done to make this one at time, but I need to make this for a crontab, so, it has to be an script, here is my lines: It counts the number of lines: egrep -i String file_name_201611* |... (5 Replies)
Discussion started by: Elly
5 Replies

10. UNIX for Beginners Questions & Answers

Awk: count unique elements in a field and sum their occurence across the entire file

Hi, Sure it's an easy one, but it drives me insane. input ("|" separated): 1|A,B,C,A 2|A,D,D 3|A,B,B I would like to count the occurence of each capital letters in $2 across the entire file, knowing that duplicates in each record count as 1. I am trying to get this output... (5 Replies)
Discussion started by: beca123456
5 Replies
Units(3pm)						User Contributed Perl Documentation						Units(3pm)

NAME
Math::Calc::Units - Human-readable unit-aware calculator SYNOPSIS
use Math::Calc::Units qw(calc readable convert equal); print "It will take ".calc("10MB/(384Kbps)")." to download "; my @alternative_descriptions = readable("10MB/(384Kbps)"); print "A week is ".convert("1 week", "seconds")." long "; if (equal("$rate bytes / sec", "1 MB/sec")) { ... }; DESCRIPTION
"Math::Calc::Units" is a simple calculator that keeps track of units. It currently handles combinations of byte sizes and duration only, although adding any other multiplicative types is easy. Any unknown type is treated as a unique user type (with some effort to map English plurals to their singular forms). The primary intended use is via the "ucalc" script that prints out all of the "readable" variants of a value. For example, "3 bytes" will only produce "3 byte", but "3 byte / sec" produces the original along with "180 byte / minute", "10.55 kilobyte / hour", etc. The "Math::Calc::Units" interface only provides for string-based computations, which could result in a large loss of precision for some applications. If you need the exact result, you may pass in an extra parameter 'exact' to "calc" or "convert", causing them to return a 2-element list containing the numerical result and a string describing the units of that result: my ($value, $units) = convert("10MB/sec", "GB/day"); (In scalar context, they just return the numeric value.) Examples of use o Estimate transmission rates (e.g., 10MB at 384 kilobit/sec) o Estimate performance characteristics (e.g., disk I/O rates) o Figure out how long something will take to complete I tend to work on performance-sensitive code that involves a lot of network and disk traffic, so I wrote this tool after I became very sick of constantly converting KB/sec to GB/day when trying to figure out how long a run is going to take, or what the theoretical maximum performance would be if we were 100% disk bound. Now I can't live without it. Contraindications If you are just trying to convert from one unit to another, you'll probably be better off with "Math::Units" or "Convert::Units". This module really only makes sense when you're converting to and from human-readable values. AUTHOR
Steve Fink <sfink@cpan.org> SEE ALSO
ucalc, Math::Units, Convert::Units. perl v5.10.0 2009-08-04 Units(3pm)
All times are GMT -4. The time now is 11:01 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy