Solved

3 column sum

Posted on 2014-03-28
4
165 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
  • 2
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

744 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

9 Experts available now in Live!

Get 1:1 Help Now