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
397 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This is about my first experience with programming Arduino.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

630 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