Solved

3 column sum

Posted on 2014-03-28
4
172 Views
Last Modified: 2014-03-30
I have a log file that I would like to sum columns 2, 3, and 4 all together. I know this can be done using awk in a shell. I have to imagine that perl has a way to do this as as a fairly straight forward operation?

If the log file was:

X 1 1 1 blah
X 1 1 1 blah
X 1 1 1 blah

The result would just be "9", for instance.
0
Comment
Question by:stakor
[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
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 39963354
Try this:
perl -lne "BEGIN { my $t = 0; } chomp; split ' '; $t += $_[1] + $_[2] + $_[3]; END { print $t; }" < test.log

Open in new window

0
 

Author Comment

by:stakor
ID: 39963374
It returned an error. I am running it on linux.

perl -lne "BEGIN { my $t = 0; } chomp; split ' '; $t += $_[1] + $_[2] + $_[3]; END { print $t; }" < ./test.log

syntax error at -e line 1, near "my  ="
BEGIN not safe after errors--compilation aborted at -e line 1.
0
 

Author Comment

by:stakor
ID: 39963548
Seems to work:

#!/usr/bin/perl

use strict;
use warnings;

open my $fh, '<', "test.log" or die $!;

my $count1 = 0;
my $count2 = 0;
my $count3 = 0;

while (<$fh>) {

    my ($j1, $c1, $c2, $c3, $j2) = split;

    $count1 += $c1;
    $count2 += $c2;
    $count3 += $c3;

}

my $total = $count1 + $count2 + $count3;
print "$total\n";

Open in new window

0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39963589
ah, I run on windows. You can still use the one liner but swap the single and double quotes around.

The 'perl -nle' is like a shorthand for the loop and other stuff.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Six Sigma Control Plans

724 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