script to find the average number or files?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to find the average number or files?
# 1  
Old 03-27-2008
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  
Old 03-27-2008
As it happens, I just wrote one.

Code:
vnix$ cat ~/bin/avg
#!/usr/bin/perl -lan

my $sum = 0.0;
map { $sum += $_ } grep { /^(\d+(\.\d+)?|\.\d+)$/ } @F;
print "$0: $sum / ", scalar (@F), " = ", $sum / @F if @F;

You'd type in numbers separated by space and hit enter when you're done.

Last edited by era; 03-27-2008 at 01:36 PM.. Reason: Usage instructions
# 3  
Old 03-27-2008
Quote:
Originally Posted by bbbngowc
Anyone has a script or command in UNIX that can take 4 to five different numbers and calculate the average?
Code:
# echo "1 2 3 4 6" | awk '{for(i=1;i<=NF;i++) t+=$i}END{print t/NF}'
3.2

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. Shell Programming and Scripting

AWK script to split data and find average

Input: 2.58359023380340e+02 1.43758864405595e+02 -7.65700666212508e+00 1.06460208083228e+02 1.26185441783936e+02 -3.41389169427027e+01 -1.40393299309592e+02 -3.07758776849508e+01 1.45067703495838e+02 1.79405834959073e+02 5.06666234594205e+01 OUT 2.0105894389e+02 (average of... (8 Replies)
Discussion started by: chrisjorg
8 Replies

3. Shell Programming and Scripting

awk based script to find the average of all the columns in a data file

Hi All, I need the modification for the below mentioned code (found in one more post https://www.unix.com/shell-programming-scripting/27161-script-generate-average-values.html) to find the average values for all the columns(but for a specific rows) and print the averages side by side. I have... (4 Replies)
Discussion started by: ks_reddy
4 Replies

4. Shell Programming and Scripting

Bash script to find the number of files and identify which ones are 0 bytes.

I am writing a bash script to find out all the files in a directory which are empty. I am running into multiple issues. I will really appreciate if someone can please help me. #!/bin/bash DATE=$(date +%m%d%y) TIME=$(date +%H%M) DIR="/home/statsetl/input/civil/test" ... (1 Reply)
Discussion started by: monasharma13
1 Replies

5. 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

6. 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

7. Shell Programming and Scripting

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 (7 Replies)
Discussion started by: nica
7 Replies

8. Shell Programming and Scripting

How to do a find number of files in awk

Hi, How can i able to do a similar operation to "find" number of files in a folder in an awk? In bash, we could do easily using "find" command. But currently, I am having an awk block and i wanted to extract these information. Please advise. Thanks. (2 Replies)
Discussion started by: ahjiefreak
2 Replies

9. UNIX for Dummies Questions & Answers

how to find the number of files

hi, 1) I want to find from the current directory the number of files (count) with two or more particular extentions in another directory. For ex: If I am in a/b/c directory... I want to find the count of the files with .txt and .dat extention in a/d/e directory. I have tried using the below... (5 Replies)
Discussion started by: harish409
5 Replies

10. Shell Programming and Scripting

awk script to find the number of files

awk script to find the number of files in a directory with their date less than 15-oct-2006 please help (4 Replies)
Discussion started by: uni_ajay_r
4 Replies
Login or Register to Ask a Question