Solved

perl or shell to sum columns 2-6 into a single column 2, create 3rd column 12 month binned avg

Posted on 2012-03-16
2
386 Views
Last Modified: 2012-06-27
See attached input file.

assuming the file perl file is called aceavg.pl or the shell script file is called aceavg.sh and creates an output file called aceavg.txt and I want to called the the script by either

perl aceavg.pl > aceavg.txt

or

sh aceavg.sh > aceavg.txt

I want to use the input file found at /path/ace.txt and simply do 2 things. 1st, sum columns 2-6 into a single column 2. 2nd, I want to create a 3rd column with 12 month rolling binned averages. So the third column won't start until month 12 since 12 data points are needed so rows 1-11 will only have two columns of data and the 3rd column of data begins from row 12. See input file.
ace.txt
0
Comment
Question by:libertyforall2
2 Comments
 
LVL 31

Accepted Solution

by:
farzanj earned 500 total points
ID: 37732512
This script test script that sums that columns you need.  If you explain how you need the binned average, I would be in a position to give you the remaining part.  Try running this:


#!/usr/bin/perl

use warnings;
use strict;

my @dat = grep {/\d\d\/\d\d\/\d{4}/} <>;
my @vals;
foreach my $row (@dat)
{
    my @col = split(/ /, $row);
    my $sum = 0;
    $sum  += $col[$_] for (1..5);
    push (@vals,"$col[0] $sum");
}
$" = "\n";
print "@vals", "\n";

Open in new window

0
 

Author Closing Comment

by:libertyforall2
ID: 37780013
Issue resolved.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question