print running field average for a set of lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print running field average for a set of lines
# 1  
Old 09-17-2010
print running field average for a set of lines

Hi everyone,

I have a program that generates logs that contains sections like this:
Code:
IMAGE INPUT
     81   0  0.995  2449470    0   1726     368     1  0.0635  0.3291
     82   0  1.001  2448013    0   1666     365     1  0.0649  0.3235
     83   0  1.009  2444822    0   1697     371     1  0.0661  0.2888
     84   0  1.004  2447224    0   1733     362     1  0.0664  0.3346
     85   0  1.011  2446792    0   1704     357     1  0.0665  0.3024
     86   0  1.016  2450130    0   1675     362     1  0.0660  0.3208
     87   0  1.018  2448809    0   1719     349     2  0.0669  0.2914
     88   0  1.017  2448947    0   1710     357     2  0.0677  0.3044
     89   0  1.028  2447439    0   1721     368     2  0.0643  0.3164

  ACCEPTED

There is also lots of text in between these sections. I've created a little script that launches the program and then parses the output into something more human-friendly.

Code:
prog_name | tee $log | awk '\
/ACCEPTED/ {
    printf "images %2d - %2d and average is %2d \n", x, y, tot/n}
/IMAGE/ {
    f=1;getline;x=$1} NF {y=$1}
{tot += $7; ++n} 
'

What I would like to get is output like this:
Code:
images  81 - 89 and average is 362
images  90 - 98 and average is 387

where the "average" is the mean of column 7 of that particular section of the log, corresponding to that set of image numbers.

What I get instead is:
Code:
images 81 - 89 and average is 27994 
images 90 - 98 and average is 17139

Any thoughts on where I've gone wrong in my script?
# 2  
Old 09-17-2010
As i already said on the other post :
Code:
awk '/ACCEPTED/{print "processing images "x" - "y" Average is "int(tot/nb)}/IMAGE/{getline;tot=nb=0;x=$1}$7>0{tot+=$7;nb++}NF{y=$1}' file

This User Gave Thanks to Chirel For This Post:
# 3  
Old 09-17-2010
See if this work for you:
Code:
# cat sample.file
 IMAGE INPUT
     81   0  0.995  2449470    0   1726     368     1  0.0635  0.3291
     82   0  1.001  2448013    0   1666     365     1  0.0649  0.3235
     83   0  1.009  2444822    0   1697     371     1  0.0661  0.2888
     84   0  1.004  2447224    0   1733     362     1  0.0664  0.3346
     85   0  1.011  2446792    0   1704     357     1  0.0665  0.3024
     86   0  1.016  2450130    0   1675     362     1  0.0660  0.3208
     87   0  1.018  2448809    0   1719     349     2  0.0669  0.2914
     88   0  1.017  2448947    0   1710     357     2  0.0677  0.3044
     89   0  1.028  2447439    0   1721     368     2  0.0643  0.3164
     91   0  0.995  2449470    0   1726     368     1  0.0635  0.3291
     92   0  1.001  2448013    0   1666     365     1  0.0649  0.3235
     93   0  1.009  2444822    0   1697     371     1  0.0661  0.2888
     94   0  1.004  2447224    0   1733     962     1  0.0664  0.3346
     95   0  1.011  2446792    0   1704     357     1  0.0665  0.3024
     96   0  1.016  2450130    0   1675     362     1  0.0660  0.3208
     97   0  1.018  2448809    0   1719     849     2  0.0669  0.2914
     98   0  1.017  2448947    0   1710     357     2  0.0677  0.3044
     99   0  1.028  2447439    0   1721     368     2  0.0643  0.3164

  ACCEPTED

Code:
# cat awk.script
function p(xx,xxx,y,c){
        printf "images %2d - %2d and average is %2d \n",xx,xxx,y/c
                        }
$1+0{
        if(x&&x!=substr($1,0,1))
                {
                p(xx,xxx,y,c)
                xx=xxx=""
                y=c=0}
                x=substr($1,0,1)
                if(!xx){
                        xx=$1
                        }
                xxx=$1
                y+=$7
                c++
                }
END             {
                p(xx,xxx,y,c)
                }
# awk -f awk.script sample.file
images 81 - 89 and average is 362
images 91 - 99 and average is 484

This User Gave Thanks to danmero For This Post:
# 4  
Old 09-17-2010
tough-looking little script! thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk - If field value of consecutive records are the identical print portion of lines

I have some data that looks like this: PXD= ZW< 1,6 QR> QRJ== 1,2(5) QR> QRJ== 4,1(2) QR> QRJ== 4,2 QRB= QRB 4,2 QWM QWM 6,2 R<C ZW< 11,2 R<H= R<J= 6,1 R>H XZJ= 1,2(2) R>H XZJ= 2,6(2) R>H XZJ= 4,1(2) R>H XZJ= 6,2 RDP RDP 1,2 What I would like to do is if fields $1 and $2 are... (5 Replies)
Discussion started by: jvoot
5 Replies

2. UNIX for Beginners Questions & Answers

Print lines based upon unique values in Nth field

For some reason I am having difficulty performing what should be a fairly easy task. I would like to print lines of a file that have a unique value in the first field. For example, I have a large data-set with the following excerpt: PS003,001 MZMWR/ L-DWD// * PS003,001... (4 Replies)
Discussion started by: jvoot
4 Replies

3. Shell Programming and Scripting

Print field after pattern in all lines

data: hello--hello1--hello2--#growncars#vello--hello3--hello4--jello#growncars#dello--gello--gelloA--gelloB#growncars# I want to be able to print all the values that are found between the patterns "#growncars#" and the next "#growncars#" on the same line. so the output should be: ... (8 Replies)
Discussion started by: SkySmart
8 Replies

4. Shell Programming and Scripting

Print ALL lines except if field is 999

Hi All!!! :-) I need a command that will print each line of a text file UNLESS the 3rd field of that line is equal to the number 999. (space seperated fields) Solaris10/BASH SHELL: INPUT.TXT aaa bbb 111 222 ccc ddd 333 444 eee fff 999 555 ggg hhh 666 777 aaa bbb 999 222 ccc ddd 333... (7 Replies)
Discussion started by: ajp7701
7 Replies

5. Shell Programming and Scripting

Combine identical lines and average the one variable field

I have the following file 299899 chrX_299716_300082 196 78.2903 299991 chrX_299982_300000 18.2538 Tajd:0.745591 FayWu:-0.245701 T2:1.45 299899 chrX_299716_300082 196 78.2903 299991 chrX_299982_300000 18.2538 Tajd:0.745591 FayWu:-0.245701 T2:0.283 311027 chrX_310892_311162 300 91.6452... (2 Replies)
Discussion started by: jfern
2 Replies

6. Shell Programming and Scripting

Awk: print lines with one of multiple pattern in the same field (column)

Hi all, I am new to using awk and am quickly discovering what a powerful pattern-recognition tool it is. However, I have what seems like a fairly basic task that I just can't figure out how to perform in one line. I want awk to find and print all the lines in which one of multiple patterns (e.g.... (8 Replies)
Discussion started by: elgo4
8 Replies

7. Linux

Perl program to print previous set of lines once a pattern is matched

Hi all, I have a text data file. My aim here is to find line called *FIELD* AV for every record and print lines after that till *FIELD* RF. But here I want first 3 to four lines for very record as well. FIELD AV is some where in between for very record. SO I am not sure how to retrieve lines in... (2 Replies)
Discussion started by: kaav06
2 Replies

8. Shell Programming and Scripting

Print specific lines of a repeated set of data

I have a file that needs 1st line, 2nd line, and 26th line printed from every chunk of data. Each chunk of data contains 26 lines (#line+%line+24 data lines = 26 lines of data repeated). Input file: # This is a data file used for blockA (chunk 1). % 10576 A 10 0 1 04 (data1) 03 (data2)... (2 Replies)
Discussion started by: morrbie
2 Replies

9. Shell Programming and Scripting

Using awk, print all the lines where field 8 is equal to x

Using awk, print all the lines where field 8 is equal to x I really did try, but this awk thing is really hard to figure out. file1.txt"Georgia","Atlanta","2011-11-02","x","","","","" "California","Los Angeles","2011-11-03","x","","","",""... (2 Replies)
Discussion started by: charles33
2 Replies

10. Shell Programming and Scripting

Print lines where there's no indent on the first field

Hi All, I need a code to print those lines where there's NO indents on the 1st field Example shown below. I tried to use the below codes but i am not able to see the expected result. Can any expert give any advise ? My Code cat filename| awk '$1 ~ /^+$/ {print $0}' Input 1199 ... (7 Replies)
Discussion started by: Raynon
7 Replies
Login or Register to Ask a Question