Solved

3 column sum

Posted on 2014-03-28
4
171 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

Independent Software Vendors: 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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

696 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