Solved

AWK totals

Posted on 2009-05-06
1
219 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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 …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…

696 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