Solved

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

Posted on 2012-03-15
2
303 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
Comment Utility
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 500 total points
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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 this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

763 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

11 Experts available now in Live!

Get 1:1 Help Now