Solved

AWK totals

Posted on 2009-05-06
1
216 Views
Last Modified: 2012-05-06
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
Comment
Question by:AndreeaN
1 Comment
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 24318693
{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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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