Solved

3 column sum

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Strange perl issue 6 128
Perl efficient DB Call 8 87
Search in text file in column and compare 4 47
To run CGI/perl on tomcat and connect to oracle database. 5 126
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

776 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