awk to find the avg of every 3 rows but only show last result?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to find the avg of every 3 rows but only show last result?
# 1  
Old 06-25-2014
awk to find the avg of every 3 rows but only show last result?

Hi,

I've got as far as this:
Code:
awk '{sum+=$1}(NR%3==1){avg=sum/3; print avg}' input.txt

Input it:
Code:
0.1
txt
txt
0.2
txt
txt
0.3
txt
txt

So, the I get the results:

Code:
0.0333333
0.133333
0.2

It's the last line (0.2) I'm interested in, basically it's the timings from cUrl I'm trying to average. Ideally I'd want to have to awk keep running for n amount of lines, if I need to up the re-running of the check.

How can I get it just to print the last result, or to pull the last result as a variable into my next if this do that nugget?

Thanks in advance, I think I broke google looking for the answer, sorry if I've slowed down your searches today (and yesterday).
Moderator's Comments:
Mod Comment Please use CODE tag for sample input and output as well as for sample code.

Last edited by Don Cragun; 06-26-2014 at 02:13 PM.. Reason: Add CODE tags.
# 2  
Old 06-25-2014
Try
Code:
awk '!(NR-1)%3 {sum=0} {sum+=$1} END {print sum/3}' file
0.2

EDIT: small but important correction:
Code:
awk '!((NR-1)%3) {sum=0} {sum+=$1} END {print sum/3}' file
0.1


Last edited by RudiC; 06-27-2014 at 07:02 AM..
# 3  
Old 06-25-2014
Thanks,

I think I'd gone AWK-blind, I can see what's happening now, tell it the lines you're interested in, sum those lines, END to get a total? Then divide by 3.

Thanks again.
# 4  
Old 06-25-2014
END is a special code section which runs only after all input files have finished reading.
# 5  
Old 06-26-2014
Slight change....

My input file is now:

Code:
0.10
200
txt
0.20
200
txt
0.30
200
txt


How can I skip out the 200's? I currently get 200.20 as the result?

Last edited by Don Cragun; 06-26-2014 at 02:14 PM.. Reason: Add CODE tags.
# 6  
Old 06-26-2014
Show us the code you're using now.
# 7  
Old 06-27-2014
I'm afraid that title plus specification in post #1 are misleading, at least for me who is not a native speaker. Trying to infer the correct meaning, I'd propose
Code:
awk '!((NR-1)%3) {sum+=$1} END {print sum/3}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

When comparing binary files, show human readable result?

Hello. I am comparing two binary file. The first file is the source file. The second file is a modified version of the first one. Modification concern uuid value. Example first file have multiple occurrences of 69a3604b-ac2b-43b7-af84-0a4a67fc6962 second file have the same occurence... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Execute command and show result in web page

Hi everyone, I have two question 1- I want to execute command in shell and after execution result show in a web server. (kind of making UI ) e.g. in shell root ~: show list item1 item2 item(n)in web server in a page draw a table and show those items in itno | name... (1 Reply)
Discussion started by: indeed_1
1 Replies

3. UNIX for Dummies Questions & Answers

Show 'All Files' If all file can find.

fileset='APP11.txt APP12.txt SPACEDB11.txt WEB11.txt WEB12.txt' Hi there, Give the fileset, if I can use grep to find all the files. How do I show: All Servers: instead of APP11.txt: APP12.txt: SPACEDB11.txt: WEB11.txt: WEB12.txt: (6 Replies)
Discussion started by: alvinoo
6 Replies

4. Shell Programming and Scripting

Get the min avg and max with awk

aaa: 3 ms aaa: 2 ms aaa: 5 ms aaa: 10 ms .......... to get the 3 2 5 10 ...'s min avg and max something like min: 2 ms avg: 5 ms max: 10 ms (2 Replies)
Discussion started by: yanglei_fage
2 Replies

5. Shell Programming and Scripting

Avg using awk

Coins: gold 1 1986 USA American Eagle gold 1 1908 Austria-Hungary Franz Josef 100 Korona silver 10 1981 USA ingot gold 1 1984 Switzerland ingot gold 1 1979 RSA Krugerrand gold 0.5 1981 RSA Krugerrand gold 0.1 1986 PRC Panda silver 1 1986 USA Liberty dollar gold 0.25 1986 USA Liberty... (3 Replies)
Discussion started by: Ramesh M
3 Replies

6. UNIX for Dummies Questions & Answers

[diff] hide missing rows, show similar

Hi all! Having the following two csv files: file1 AAA;0000;RED CCC;9900;GREEN file2 AAA;0000;BLACK BBB;0099;BLU What's the correct syntax to hide only the missing rows (BBB,CCC) and show the rows that differ only with last field? I expect something like this: diff <options> file1... (2 Replies)
Discussion started by: Evan
2 Replies

7. Shell Programming and Scripting

Comparing rows in same file and writing the result in new file

Help needed... Can you tell me how to compare the last two couple entries in a file and print their result in new file..:confused: I have one file Check1.txt \abc1 12345 \abc2 12327 \abc1 12345 \abc2 12330 I want to compare the entries in Check1 and write to... (1 Reply)
Discussion started by: kichu
1 Replies

8. Shell Programming and Scripting

Find avg using awk

Hi, i need some help plz... The file data.txt contains: code of student,surname and name,code of lesson,grade of lesson.The number of lessons of each student is not the same. 25,Jackson Steve,12,4,34,2,65,2 29,Jordan Mary,13,6,23,8,56,4,34,2 04,Leven Kate,14,6,15,6,26,4 34,Owen... (10 Replies)
Discussion started by: Steve_09
10 Replies

9. UNIX for Dummies Questions & Answers

how to use find commnad to show only path of the result

Hello all say i like to find files i do : find . -name "*.txt" but if i like to find ( and print out ) only the path's where the files are ( the *.txt files ) what can i add to the find command ? (1 Reply)
Discussion started by: umen
1 Replies

10. Shell Programming and Scripting

Show result only if number is greater then

Hello all Im trying to write one liner that will show me results only if the result of the expression is greater then 0 For example: I do : find . -name "*.dsp" | xargs grep -c SecurityHandler the result are : ./foo/blah/a.dsp:0 ./foo/blah1/b.dsp:1 ./foo/blah2/c.dsp:2... (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question