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
384 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
Comment Utility
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
Comment Utility
Issue resolved.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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 …

728 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now