[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

3 column sum

Posted on 2014-03-28
4
Medium Priority
?
174 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Six Sigma Control Plans

650 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