Operating System - Linux
1752590 Members
3925 Online
108788 Solutions
New Discussion юеВ

Re: awk to perl (sum on columns)

 
SOLVED
Go to solution
Leo The Cat
Regular Advisor

awk to perl (sum on columns)

Hi

I wrote this small awk script. Objective is to sum columns...

echo $(cat $OUTPUT_FILE | awk 'NR > 0 {
countN1 += $2
countN2 += $3
}

END {
print "### countN1 countN2 "
}') >> $OUTPUT_FILE

How to do the same things but with perl ?

Bests Regards
Den
2 REPLIES 2
James R. Ferguson
Acclaimed Contributor
Solution

Re: awk to perl (sum on columns)

Hi Den:

One way:

perl -nale 'next if $.<2;$count1+=$F[1];$count2+=$F[2];END{print "### $count1 $count2"}' file

Remember, 'awk' numbers fields starting with one (1). Perl is more mathematical and starts at zero (0). The '-a' means "auto-split" into an array '@F'. The '-n' gives us a read loop for the file(2) specified as an argument. The '-l' adds a linefeed to each output line and the '-e' signals the script follows.

Regards!

...JRF...
Leo The Cat
Regular Advisor

Re: awk to perl (sum on columns)

Thanks again James! Perfect !
Bests Regards
Den