Solved

AWK totals

Posted on 2009-05-06
1
210 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
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 …
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now