Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
408 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

688 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