Average number of each line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Average number of each line
# 1  
Old 12-11-2008
Average number of each line

Hi,

Please, can anyone help me how to get the average number of each line using awk

Input File:
60 30 23 66 42
32 48 49 27 33
44 22 88 11 21
73 24 38 66 71


Desired Output:
44.2
37.8
37.2
54.4


Thanks
# 2  
Old 12-11-2008
Code:
awk '{for(i=1;i<=NF;i++) s+=$i;printf("%.1f\n", s/NF)}' myFile

# 3  
Old 12-11-2008
Thanks vgersh99,

Actually, I have written the code very similar to your code but it is not produce the desired output.

here, the output of your code:
44.2
82.0
119.2
173.6
awk: cmd. line:1: (FILENAME=test FNR=5) fatal: division by zero attempted
# 4  
Old 12-11-2008
you must have an 'empty' line in your input file. try this:
Code:
awk 'NF {for(i=1;i<=NF;i++) s+=$i;printf("%.1f\n", s/NF)}' myFile

# 5  
Old 12-11-2008
Quote:
Originally Posted by nica
Thanks vgersh99,

Actually, I have written the code very similar to your code but it is not produce the desired output.

here, the output of your code:
44.2
82.0
119.2
173.6
awk: cmd. line:1: (FILENAME=test FNR=5) fatal: division by zero attempted
you have any empty line at the end of the file try the vgersh99 new code
# 6  
Old 12-11-2008
Hi,

I have try your new code but the output does not show the average of each line unless only for the first line show the correct average

Input File:
60 30 23 66 42 ---> Avg=44.2
32 48 49 27 33----->Avg=37.8
44 22 88 11 21 ---->Avg=37.2
73 24 38 66 71---->Avg=54.4

Desired Output:
44.2
37.8
37.2
54.4
# 7  
Old 12-11-2008
my bad - sorry:
Code:
awk 'NF {s=0;for(i=1;i<=NF;i++) s+=$i;printf("%.1f\n", s/NF)}' myFile

This User Gave Thanks to vgersh99 For This Post:
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 or sed script to count number of occurrences and creating an average

Hi Friends , I am having one problem as stated file . Having an input CSV file as shown in the code U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_2_/Q,1,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,0... (4 Replies)
Discussion started by: kshitij
4 Replies

2. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

3. Shell Programming and Scripting

Average calculation based on number of rows

Dear users, I need your support, I have a file like this: 272134.548 6680572.715 272134.545 6680572.711 272134.546 6680572.713 272134.548 6680572.706 272134.545 6680572.721 272134.543 6680572.710 272134.544 6680572.715 272134.543 6680572.705 272134.540 6680572.720 272134.544... (10 Replies)
Discussion started by: Gery
10 Replies

4. Shell Programming and Scripting

Script to find the average of a given column and also for specified number of rows?

Hi Friends, In continuation to my earlier post https://www.unix.com/shell-programming-scripting/99166-script-find-average-given-column-also-specified-number-rows.html I am extending my problem as follows. Input: Column1 Column2 MAS 1 MAS 4 ... (2 Replies)
Discussion started by: ks_reddy
2 Replies

5. Shell Programming and Scripting

Script to find the average of a given column and also for specified number of rows??

Hi friends I have 100 files in my directory. Each file look like this.. Temp1 Temp2 Temp3 MAS 1 2 3 MAS 4 5 6 MAS 7 8 9 Delhi 10 11 12 Delhi 13 14 15 Delhi 16 17 ... (4 Replies)
Discussion started by: ks_reddy
4 Replies

6. UNIX for Dummies Questions & Answers

Calculating the Number of Rows and Average

Hi All I like to know how can we calculate the number of rows and the average of the values present in the file. I will not know what will be the rowcount, which will be dynamic in nature of the file. eg. 29 33 48 30 28 (6 Replies)
Discussion started by: pk_eee
6 Replies

7. Shell Programming and Scripting

how to get the data from line number 1 to line number 100 of a file

Hi Everybody, I am trying to write a script that will get some perticuler data from a file and redirect to a file. My Question is, I have a Very huge file,In that file I have my required data is started from 25th line and it will ends in 100th line. I know the line numbers, I need to get all... (9 Replies)
Discussion started by: Anji
9 Replies

8. Shell Programming and Scripting

Adding a columnfrom a specifit line number to a specific line number

Hi, I have a huge file & I want to add a specific text in column. But I want to add this text from a specific line number to a specific line number & another text in to another range of line numbers. To be more specific: lets say my file has 1000 lines & 4 Columns. I want to add text "Hello"... (2 Replies)
Discussion started by: Ezy
2 Replies

9. Shell Programming and Scripting

script to find the average number or files?

Anyone has a script or command in UNIX that can take 4 to five different numbers and calculate the average? (2 Replies)
Discussion started by: bbbngowc
2 Replies

10. Shell Programming and Scripting

Appending line number to each line and getting total number of lines

Hello, I need help in appending the line number of each line to the file and also to get the total number of lines. Can somebody please help me. I have a file say: abc def ccc ddd ffff The output should be: Instance1=abc Instance2=def Instance3=ccc Instance4=ddd Instance5=ffff ... (2 Replies)
Discussion started by: chiru_h
2 Replies
Login or Register to Ask a Question