• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

AWK totals

I am very new to awk, so I am hoping this should be relatively straightforward:
I have a text file with 4 columns and only 5 rows, and I am calling this little script when I want to display only two of the columns, and add two totals at the bottom. What I am trying is below, but the Total is coming out to 0, and the Average is writing 13100, which is just not correct (should be 50000ish)

The desired output is:
column1data     column4data
column1data     column4data
.
.
Total                  <sumofcolumn4>
Average         <sumofcolumn4/5>

I would like to know where I am going wrong and how to fix it.
Many thanks
BEGIN 
{total = sum $4} 
{average = sum $4 / $5}
{printf"%-12s\t %-12s\n", $1, $4}
 
END { 
{printf "%-12s\t %-12d\n", "Total", total}
{printf "%-12s\t %-12d\n", "Average", average}
}

Open in new window

0
AndreeaN
Asked:
AndreeaN
1 Solution
 
ozoCommented:
{printf"%-12s\t %-12s\n", $1, $4}
{total += $4}
{count += $5}

END {
  printf "%-12s\t %-12d\n", "Total", total;
  printf "%-12s\t %-12f\n", "Average", total/count;
}
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now