Sponsored Content
Top Forums UNIX for Dummies Questions & Answers calculate average of column 2 Post 302125743 by onthetopo on Friday 6th of July 2007 08:16:22 PM
Old 07-06-2007
calculate average of column 2

Hi I have fakebook.csv as following:

F1(current date) F2(popularity) F3(name of book) F4(release date of book)
2006-06-21,6860,"Harry Potter",2006-12-31
2006-06-22,,"Harry Potter",2006-12-31
2006-06-23,7120,"Harry Potter",2006-12-31
2006-06-24,,"Harry Potter",2006-12-31
2006-06-25,7498,"Harry Potter",2006-12-31
2006-06-26,7500,"Harry Potter",2006-12-31
2006-06-27,7750,"Harry Potter",2006-12-31
2006-06-28,7860,"Harry Potter",2006-12-31
2006-06-29,,"Harry Potter",2006-12-31
2006-06-30,7860,"Harry Potter",2006-12-31
2006-07-01,1760,"Bible",2007-03-03
2006-07-02,2755,"Bible",2007-03-03
2006-07-03,3760,"Bible",2007-03-03
2006-07-04,,"Bible",2007-03-03
2006-07-05,5788,"Bible",2007-03-03
2006-07-06,6799,"Bible",2007-03-03
...............................

I want to add a new column in each line that is Field 5 (average popularity[over a sample of three days] 6 months before release date)

So for the harry potter book, the release date is 2006-12-31, I need to find the average value of field 2 over the following date points: 2006-06-30,2006-06-28 and 2006-06-27. (This problem is complicated by the fact that the popularity of 2006-06-29 is unavailable and left as blank in the csv, so 2006-06-29 is skipped in the calculation of average).

My supervisor gave me a short timespan to solve this.I've already got the datecalc script found in FAQ but haven't found its use in my context yet. Can someone please help me? Thanks a million!
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Use awk to calculate average of column 3

Suppose I have 500 files in a directory and I need to Use awk to calculate average of column 3 for each of the file, how would I do that? (6 Replies)
Discussion started by: grossgermany
6 Replies

2. Programming

calculate average

I have a file which is 2 3 4 5 6 6 so i am writing program in C to calculate mean.. #include<stdio.h> #include<string.h> #include <math.h> double CALL mean(int n , double x) main (int argc, char **argv) { char Buf,SEQ; int i; double result = 0; FILE *fp; (3 Replies)
Discussion started by: cdfd123
3 Replies

3. Shell Programming and Scripting

Calculate average time using a script

Hello, I'm hoping to get some help on calculating an average time from a list of times (hour:minute:second). Here's what my list looks like right now, it will grow (I can get the full date or change the formatting of this as well): 07:55:31 09:42:00 08:09:02 09:15:23 09:27:45 09:49:26... (4 Replies)
Discussion started by: jaredhanks
4 Replies

4. Shell Programming and Scripting

AWK novice - calculate the average

Hi, I have the following data in a file for example: P1 XXXXXXX.1 YYYYYYY.1 ZZZ.1 P1 XXXXXXX.2 YYYYYYY.2 ZZZ.2 P1 XXXXXXX.3 YYYYYYY.3 ZZZ.3 P1 XXXXXXX.4 YYYYYYY.4 ZZZ.4 P1 XXXXXXX.5 YYYYYYY.5 ZZZ.5 P1 XXXXXXX.6 YYYYYYY.6 ZZZ.6 P1 XXXXXXX.7 YYYYYYY.7 ZZZ.7 P1 XXXXXXX.8 YYYYYYY.8 ZZZ.8 P2... (6 Replies)
Discussion started by: alex2005
6 Replies

5. Shell Programming and Scripting

Calculate Average AWK

I want to calculate the average line by line of some files with several lines on them, the files are identical, just want to average the 3rd columns of those files.:wall: Example file: File 1 001 0.046 0.667267 001 0.047 0.672028 001 0.048 0.656025 001 0.049 ... (2 Replies)
Discussion started by: AriasFco
2 Replies

6. Shell Programming and Scripting

Calculate the average of a column based on the value of another column

Hi, I would like to calculate the average of column 'y' based on the value of column 'pos'. For example, here is file1 id pos y c 11 1 220 aa 11 4333 207 f 11 5333 112 ee 11 11116 305 e 11 11117 310 r 11 22228 781 gg 11 ... (2 Replies)
Discussion started by: jackken007
2 Replies

7. Shell Programming and Scripting

Calculate Average time of one column

Hello dears, I have a log file with records like below and want to get a average of one column based on the search of one specific keyword. 2015-02-07 08:15:28 10.102.51.100 10.112.55.101 "kevin.c" POST ... (2 Replies)
Discussion started by: Newman
2 Replies

8. Shell Programming and Scripting

Match first two columns and calculate percent of average in third column

I have the need to match the first two columns and when they match, calculate the percent of average for the third columns. The following awk script does not give me the expected results. awk 'NR==FNR {T=$3; next} $1,$2 in T {P=T/$3*100; printf "%s %s %.0f\n", $1, $2, (P>=0)?P:-P}' diff.file... (1 Reply)
Discussion started by: ncwxpanther
1 Replies

9. Shell Programming and Scripting

Calculate average, azimut and distance

Gents, Please i will to get the distance and azimut from 2 coordinates: Usig excel formula i get the correct values, but i will like to do it using awk. Example A 35089.0 50345.016 9 75 1 2101774 77 70 79 483911.6 2380106.9 137.4 1 1 6 1 A 35089.0 50345.01620 75... (8 Replies)
Discussion started by: jiam912
8 Replies

10. Shell Programming and Scripting

Calculate the average per block.

My old school way is a one liner. And will search for average from SAR, to get the data receive rate. But, I dont think it is practical or accurate,. Because it calculates to off peak hours. I am planning to change it. My cron runs every 30 mins. When my cron runs, and my time is 14:47pm,, it will... (1 Reply)
Discussion started by: invinzin21
1 Replies
List::Maker(3pm)					User Contributed Perl Documentation					  List::Maker(3pm)

NAME
List::Maker - Generate more sophisticated lists than just $a..$b VERSION
This document describes List::Maker version 0.0.5 SYNOPSIS
use List::Maker; @list = <1..10>; # (1,2,3,4,5,6,7,8,9,10) @list = <10..1>; # (10,9,8,7,6,5,4,3,2,1) @list = <1,3,..10>; # (1,3,5,7,9) @list = <1..10 x 2>; # (1,3,5,7,9) @list = <0..10 : prime N>; # (2,3,5,7) @list = <1,3,..30 : /7/>; # (7,17,27) @list = < ^10 >; # (0,1,2,3,4,5,6,7,8,9) @list = < ^@array >; # (0..$#array) @words = < a list of words >; # ('a', 'list', 'of', 'words') @words = < 'a list' "of words" >; # ('a list', 'of words') use List::Maker 'listify'; @list = listify '1..10'; # (1,2,3,4,5,6,7,8,9,10) use List::Maker 'make_list'; @list = make_list '10..1'; # (10,9,8,7,6,5,4,3,2,1) use List::Maker 'ql'; @list = ql'1..10 x 2'; # (1,3,5,7,9) DESCRIPTION
The List::Maker module hijacks Perl's built-in file globbing syntax ("< *.pl >" and "glob '*.pl'") and retargets it at list creation. The rationale is simple: most people rarely if ever glob a set of files, but they have to create lists in almost every program they write. So the list construction syntax should be easier than the filename expansion syntax. Alternatively, you can load the module with an explicit name, and it creates a subroutine of that name that builds the same kinds of lists for you (leaving the globbing mechanism unaltered). INTERFACE
Within any file in which the module has been explicitly loaded: use List::Maker; angle brackets no longer expand a shell pattern into a list of files. Instead, they expand a list specification into a list of values. Numeric lists Numeric list specifications may take any of the following forms: Type Syntax For example Produces ========== =================== =========== =========== Count up <MIN..MAX> <1..5> (1,2,3,4,5) Count down <MAX..MIN> <5..1> (5,4,3,2,1) Count to < ^LIMIT > < ^5 > (0,1,2,3,4) Count by <START..END x STEP> <1..10 x 3> (1,4,7,10) Count via <START, NEXT,..END> <1, 3,..10> (1,3,5,7,9) The numbers don't have to be integers either: @scores = <0.5..4.5>; # same as: (0.5, 1.5, 2.5, 3.5, 4.5) @steps = <1..0 x -0.2>; # same as: (1, 0.8, 0.6, 0.4, 0.2, 0) Filtered numeric lists Any of the various styles of numeric list may also have a filter applied to it, by appending a colon, followed by a boolean expression: @odds = <1..100 : $_ % 2 != 0 >; @primes = <3,5..99> : is_prime($_) >; @available = < ^$max : !allocated{$_} > @ends_in_7 = <1..1000 : /7$/ > The boolean expression is tested against each element of the list, and only those for which it is true are retained. During these tests each element is aliased to $_. However, since angle brackets interpolate, it's necessary to escape any explicit reference to $_ within the filtering expression, as in the first three examples above. That often proves to be annoying, so the module also allows the candidate value to be referred to using any single uppercase letter (which is replaced with "$_" when the filter is applied. So the previous examples could also be written: @odds = <1..100 : N % 2 != 0 >; @primes = <3,5..99> : is_prime(N) >; @available = < ^$max : !allocated{N} > or (since the specific letter is irrelevant): @odds = <1..100 : X % 2 != 0 >; @primes = <3,5..99> : is_prime(I) >; @available = < ^$max : !allocated{T} > String lists Any list specification that doesn't conform to one of the four pattern described above is taken to be a list of whitespace-separated strings, like a "qw{...}" list: @words = <Eat at Joe's>; # same as: ( 'Eat', 'at', 'Joe's' ) However, unlike a "qw{...}", these string lists interpolate (before listification): $whose = q{Joe's}; @words = <Eat at $whose>; # same as: ( 'Eat', 'at', 'Joe's' ) More interestingly, the words in these lists can be quoted to change the default whitespace separation. For example: @names = <Tom Dick "Harry Potter">; # same as: ( 'Tom', 'Dick', 'Harry Potter' ) Single quotes may be also used, but this may be misleading, since the overall list still interpolates in that case: @names = <Tom Dick '$Harry{Potter}'>; # same as: ( 'Tom', 'Dick', "$Harry{Potter}" ) In a scalar context, any string list is converted to the standard English representation: $names = <Tom>; # 'Tom' $names = <Tom Dick>; # 'Tom and Dick' $names = <Tom Dick 'Harry Potter'>; # 'Tom, Dick, and Harry Potter' Perl 6 repetition list operator List::Maker also understands the Perl 6 "xx" listification operator: @affirmations = <'aye' xx 5>; # ('aye','aye','aye','aye','aye') Random number generation The module understands two syntaxes for generating random numbers. It can generate a random number within a range: $random = < 2r5.5 >; # 2 <= Random number < 5.5 It can also generate an "NdS" dice roll (i.e. the sum of rolling N dice, each with S sides): $roll = < 3d12 >; # Sum of three 12-sided dice The dice notation cares nothing for the laws of physics or rationality and so it will even allow you to specify a non-integer number of "fractal dice", each with an non-integer numbers of sides: $roll = < 3.7d12.3 >; # Sum of three-point-seven 12.3-sided dice In a list context, the dice notation returns the results of each of the individual die rolls (including the partial result of a "fractal" roll) @rolls = < 3d12 >; # (6, 5, 12) @rolls = < 3.7d12.3 >; # (6.1256, 5.9876, 12.0012, 0.3768) The values returned in list context will always add up to the value that would have been returned in a scalar context. User-defined syntax via "add_handler" You can add new syntax variations to the "<...>" format using the "add_handler()" function: add_handler($pattern => $sub_ref, $pattern => $sub_ref...); Each pattern is added to the list of syntax checkers and, if it matches, the corresponding subroutine is called to furnish the result of the "<...>". User-defined handlers are tested in the same order that they are defined, but before the standard built-in formats described above. ALTERNATE INTERFACE
If an argument is passed to the "use List::Maker" statement, then that argument is used as the name of a subroutine to be installed in the current package. That subroutine then expects a single argument, which may be used to generate any of the lists described above. In other words, passing an argument to "use List::Maker" creates an explicit list-making subroutine, rather than hijacking the built-in "<..>" and "glob()". For example: use List::Maker 'range'; for (range '1..100 x 5') { print "$_: $result{$_} "; } use List::Maker 'roll'; if (roll '3d12' > 20) { print "The $creature hits you "; } use List::Maker 'conjoin'; print conjoin @names; DIAGNOSTICS
"Sequence <%s, %s, %s...> will never reach %s" The specified numeric list didn't make sense. Typically, because you specified an increasing list with a negative step size (or vice versa). CONFIGURATION AND ENVIRONMENT
List::Maker requires no configuration files or environment variables. DEPENDENCIES
None. INCOMPATIBILITIES
Using this module normally prevents you from using the built-in behaviours of "<...>" or "glob()" in any files that directly "use" the module (though files that don't load the module are unaffected). In files that use the module, you would need to use the "File::Glob" module directly: use File::Glob; my @files = bsd_glob("*.pl"); Alternatively, export the list maker by name (see "ALTERNATE INTERFACE"). BUGS AND LIMITATIONS
The lists generated are not lazy. So this: for (<1..10000000>) { ... } will be vastly slower than: for (1..10000000) { ... } Please report any bugs or feature requests to "bug-list-maker@rt.cpan.org", or through the web interface at <http://rt.cpan.org>. AUTHOR
Damian Conway "<DCONWAY@CPAN.org>" LICENCE AND COPYRIGHT
Copyright (c) 2005, Damian Conway "<DCONWAY@CPAN.org>". All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic. DISCLAIMER OF WARRANTY
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. perl v5.10.1 2010-11-05 List::Maker(3pm)
All times are GMT -4. The time now is 10:21 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy