Operating System - HP-UX
1827394 Members
5565 Online
109965 Solutions
New Discussion

Re: totalling columns in a script

 
SOLVED
Go to solution
miracle
Frequent Advisor

totalling columns in a script

I am trying to total some columns from some output from an EMC command in a file.
Here is the output.


D E V I C E G R O U P S

Num of Num of Num of
Name Type Valid Symmetrix ID Devices GK's BCV's

goanna1_rdf RDF2 Yes 000285502576 18 0 18
koala1_rdf RDF1 Yes 000285502576 70 0 0
snake11_rdf RDF1 Yes 000285502576 26 0 0
beaver4_rdf RDF1 Yes 000285502576 5 0 0
trouser1_meta REGULAR Yes 000285502576 1 0 0
monster1_meta1 REGULAR Yes 000285502576 1 0 1
beaver2_reg REGULAR Yes 000285502576 4 0 0


I can total one column using:
$ awk 'BEGIN {total +=5;}
> END {print "Total ",total}' inputfile >outputfile

When I try to use a similar method it gives a strange total
$ awk '{F5=F5+$5;F6=F6+$6;F7=F7+$7};END {print F5,F6,F7}'inputfile
3787 458 03787 458 0

What I really want is totals for each of the last three columns with the numbers appearing underneath.
AWK would be preferable but any ideas to solve the problem will get you points.

12 REPLIES 12
Michael Steele_2
Honored Contributor

Re: totalling columns in a script

COL5=0
COL6=0
COL7=0
cat file | while read a b c d e f g
do
COL5=$(($e+$COL5))
COL6=$(($f+$COL6))
COL7=$(($g+$COL7))
done
Support Fatherhood - Stop Family Law
miracle
Frequent Advisor

Re: totalling columns in a script

When I try this I get the totals correctly but the formatting is not where I want it.
I want to append to the same input file.

$ awk '{t1 +=$5;t2 +=$6;t3 +=$7}
> END {print "Total "t1 ,t2 ,t3}' input file >>inputfile

Thanks Michael for your suggestion but it gives an error:
C+0: bad number
S.K. Chan
Honored Contributor

Re: totalling columns in a script

Try this instead ..
$ awk '{F5=F5+$5;F6=F6+$6;F7=F7+$7;} END {print "total:", F5,F6,F7}' inputfile
You got a bit of syntax problem in your original awk statement. I did not test this but I think it should work.
miracle
Frequent Advisor

Re: totalling columns in a script

Thanks Mr SK,
I had already worked that part out. I have since got it to work using this, but there must be a better way to format using spaces, which is what I've done.

cat /tmp/myfile| awk '{t1 +=$5;t2 +=$6;t3 +=$7}
END {printf("Total %d %d %d\n",t1,t2,t3)}' >>/tmp/myfile
miracle
Frequent Advisor

Re: totalling columns in a script

In between each of the %d entries, there are number of spaces included to place the totals under columns 5, 6 & 7. Is there a better way?

I don't care if I have to use something else. ... perl ... ? please help
S.K. Chan
Honored Contributor

Re: totalling columns in a script

You can pad the field with a specify width to get the format you wanted. For example ..

$ cat /tmp/... END {printf("Total 5%d8%d10%d\n",t1,t2,t3)}' .....

and you would get this output.

Total<4 spaces>125<7 spaces>0<9 spaces>19
miracle
Frequent Advisor

Re: totalling columns in a script

Mr SK

That does not help, it now prints my total line as

Total 5125801019
Michael Tully
Honored Contributor
Solution

Re: totalling columns in a script

Try it this way, by placing the number and % and numerals like this.

e.g.

cat file | awk '{t1 +=$5;t2 +=$6;t3 +=$7}
END {printf("Total %50d %8d %8d\n",t1,t2,t3)}' >>file

The padding for the spacing of the numbers has to be after the % sign not before.

Regards
Michael
"When I have trouble spelling, it's called fat finger syndrome"
Anyone for a Mutiny ?
miracle
Frequent Advisor

Re: totalling columns in a script

That works great, I was just wondering if there was a perl solution for this? Can someone help me?
Robin Wakefield
Honored Contributor

Re: totalling columns in a script

Well here's one way:

perl -ape '{$a+=$F[4];$b+=$F[5];$c+=$F[6]}END{printf("Total %50d %8d %8d\n",$a,$b,$c)}' yourfile

rgds, Robin
Mike Miller_8
Regular Advisor

Re: totalling columns in a script

simply run the awk program provided using the command

awk -f file.awk filename > outputFile

I think this will generate what you want.

= Mike =

miracle
Frequent Advisor

Re: totalling columns in a script

Thanks Robin for the perl script, that too works well. Thanks Mike for your script as well, but I was trying to limit the padding for the spaces. This case is closed.