Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

average data in third column per month and round to the nearest hundredth

Posted on 2012-03-15
2
Medium Priority
?
314 Views
Last Modified: 2012-03-16
I have a file called output.txt and I want to average data per month. The original attached file should create month in the first column average value in the second column. Each row should look like this below. The averages are bogus just for example. See attached file.

output2.txt sample file rows with bogus data.
198001 120.22
198002 133.42
output.txt
0
Comment
Question by:libertyforall2
  • 2
2 Comments
 
LVL 4

Expert Comment

by:bazika
ID: 37729176
Could You please clarify the stucture of Your input file (which is called "output.txt")?

Sometimes there are 4 columns there, and later columns have 5 columns. Which exactly fields should be analyzed?
0
 
LVL 4

Accepted Solution

by:
bazika earned 2000 total points
ID: 37729248
Here is the example of shell script.
I suppose, that the first field in output.txt is "YYYYMMDD", so selecting "substr($1, 1, 6 )" gives us "YYYYMM" (year+month).

Also, the summary of all fields from the 3-rd is taken (i.e. field3+field4+field5 ...)

I do not know, which awk version is used, therefor, I do additional initialization of the arrays.

#!/bin/ksh

cat output.txt | awk '
$3 != "" { cur_mon = substr($1, 1, 6 ) ;
        if( a_sum[cur_mon] == "" ) { a_sum[cur_mon] = 0 ; a_cou[cur_mon]  = 0 }
        for(i=3;i<=NF;i++) {
                a_sum[cur_mon] += $i;
                a_cou[cur_mon] ++ ;
        }
}
END {
        for (cur_mon in a_sum )
                { printf "%s %.2f\n", cur_mon, a_sum[cur_mon]/a_cou[cur_mon] ; }
        }
' | sort -k 1

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

927 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