Average result of text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Average result of text file
# 1  
Old 11-21-2018
Average result of text file

Hi all

I have created a script to allow me to gain a number of file times and how long they take to execute. I have been asked to get the average time for the log. The log will update every 5 minutes and never be empty.

Code:
141
152
157
161
168
169
171
179
202
207
229
651
666
714
759
795
823
845
853
863
931
978
982
1005
1063
1064
1071
1079
1107
1132
1150
1174
1212
1243
1303
1336
1378
1475
1591
1673
1697
1723
1729
1730

This is how the numbers are displayed within the text file (in milliseconds) - how do I write a script to add them all together and divide by the number of rows which could change every 5 minutes?

Incase you want too see the script I have created

Code:
get_file=`ls -rt localhost_access_log.*.txt|tail -1`
cat $get_file | tail -1000 | grep "objectId" | awk -F 'HTTP/1.1" 200' '{ print $2}'|awk -F' ' '{ print $2 }' | sort -n>/opt/product/apachetomcat/apm/epagent/epaplugins/centrica/correspondence_log_files/9080.txt

min_value=`cat /opt/product/apachetomcat/apm/epagent/epaplugins/centrica/correspondence_log_files/9080.txt|head -1`
max_value=`cat /opt/product/apachetomcat/apm/epagent/epaplugins/centrica/correspondence_log_files/9080.txt|tail -1`

# 2  
Old 11-21-2018
Your entire cat/tail/grep/awk... could be simplified and the avg calculation could incorporated into the pipeline, but as a stand-alone avg calculation given your sample file is:
Code:
awk '{s+=$0;fnr=FNR}END {printf("%.2f\n", s/fnr}' myFile


Last edited by vgersh99; 11-21-2018 at 12:09 PM..
# 3  
Old 11-22-2018
Quote:
Originally Posted by vgersh99
Your entire cat/tail/grep/awk... could be simplified and the avg calculation could incorporated into the pipeline, but as a stand-alone avg calculation given your sample file is:
Code:
awk '{s+=$0;fnr=FNR}END {printf("%.2f\n", s/fnr}' myFile

Are you able to explain this code to me not sure I understand it fully?
# 4  
Old 11-22-2018
Code:
awk '
# Code inside { } braces without other qualifiers is run once per line
{ 
        # Add the current line, $0, to the total in s.  The +0 guarantees its treated as a number, not a string.
        s+=$0+0
        # Store the current record (line) count inside the fnr variable.  FNR is a special variable meaning file record number.
        fnr=FNR
}
# This code runs once, after myFile has been processed.
END {
        # Print the sum, divided by the number of records, to two decimal places
        printf("%.2f\n", s/fnr)
}' myFile


Last edited by Corona688; 02-05-2019 at 03:27 PM..
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to parse file and display result based on text

I am trying using awk to open an input file and check a column 2/field $2 and if there is a warning then that is displayed (variantchecker): G not found at position 459, found A instead. The attached Sample1.txt is that file. If in that column/field there is a black space, then the text after... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

Hit multiple URL from a text file and store result in other test file

Hi, I have a problem where i have to hit multiple URL that are stored in a text file (input.txt) and save their output in different text file (output.txt) somewhat like : cat input.txt http://192.168.21.20:8080/PPUPS/international?NUmber=917875446856... (3 Replies)
Discussion started by: mukulverma2408
3 Replies

3. Shell Programming and Scripting

Save result to a text file

Currently I have a perl code to combine two different files. #! /usr/bin/perl -w use strict; open FP1,"A.txt"; open FP2,"B.txt"; my ($l1,$l2); while(1) { $l1=<FP1>; chomp $l1; $l2=<FP2>; chomp $l2; last unless(defined $l1 or defined $l2); print "$l1 $l2\n"; } close FP2;... (4 Replies)
Discussion started by: Tzeronone
4 Replies

4. Shell Programming and Scripting

How to output thre result to a text file?

Dear All I have a script file like the following: ..... ..... Variable_1 = 888 Variable_2 = 999 ..... MyExternalProgram Myfile ..... The line MyExternalProgram Myfile will generate some number, Say 777 Now I hope I can output the result of above codes to a text file,... (2 Replies)
Discussion started by: littlewenwen
2 Replies

5. UNIX Desktop Questions & Answers

Calculate average for rows in a text file

Dear Gurus, I have tab-delimited text files with matrix containing values. The first column is a identifier and other columns have the corresponding values. I would like to calculate the average value (total number/number of entries) for all entries from 2nd column to the last column in row... (3 Replies)
Discussion started by: Unilearn
3 Replies

6. Shell Programming and Scripting

Computing average values from multiple text files

Hi, first, I have searched in the forum for this, but I could not find the right answer. (There were some similar threads, but I was not sure how to adapt the ideas.) Anyway, I have a quite natural problem: Given are several text files. All files contain the same number of lines and the same... (3 Replies)
Discussion started by: rbredereck
3 Replies

7. Shell Programming and Scripting

Append Text in Result File Name

Hi Below command is returning the list of files which having this string "MTL_SYSTEM_ITEMS". find . -name "*"|xargs grep -il MTL_SYSTEM_ITEMS Ex: Above command is returing 2 files (Out of 10 files 2 files having this string). ./file1.txt and ./file2.txt Here I want to append... (3 Replies)
Discussion started by: balajiora
3 Replies

8. Shell Programming and Scripting

Displaying Result to a Text File

Hi; I am scripting in Shell and i want to write output (on screen) to a text file? ... | tee gcsw/output.txt doesnot work? :(:( (6 Replies)
Discussion started by: gc_sw
6 Replies

9. Shell Programming and Scripting

Extract numbers from text file work out average

Just wondering if someone could assist me with shell script I'm trying to write. I need to read the final column of a text file (shown below) and workout what the average number is. The text file will have a variable number of lines, I just want the script to pull out the values in the final field... (14 Replies)
Discussion started by: rich@ardz
14 Replies

10. Shell Programming and Scripting

Outputting formatted Result log file from old 30000 lines result log<help required>

Well I have a 3000 lines result log file that contains all the machine data when it does the testing... It has 3 different section that i am intrsted in 1) starting with "20071126 11:11:11 Machine Header 1" 1000 lines... "End machine header 1" 2) starting with "20071126 12:12:12 Machine... (5 Replies)
Discussion started by: vikas.iet
5 Replies
Login or Register to Ask a Question