Get the average of lines with the same first 4 letters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get the average of lines with the same first 4 letters
# 8  
Old 08-15-2018
Quote:
Originally Posted by kenshinhimura
Code:
awk '
function print_total() {
        printf("\nTotal Sum of %s site is: %d Mbps\n ", last, total)
}
function print_average() {
        printf("\nTotal Sum of %s site is: %d Mbps\n ", last, average)
}

last != substr($1, 1, 7) {
        if(NR > 1) {
                print_total()
                printf("\n=======================\n")
        }
        last = substr($1, 1, 7)
        total = 0
        average = 0
}
{       print
        total += $2
        average total/NR
}
END {   print_total()
        print_average()
}' hhhh > datarate.log
cat datarate.log

trying different combination but to no avail. Thank you
Since you want to print an average every time you print a total for a set, you don't need two functions to print the total and the average -- just add another printf statement in the print_total function. If you add a new function, you need to call that function when you want the code that it defines to be run. The statement in your code:
Code:
        average total/NR

doesn't assign a value to any variable and just performs two calculations throwing away the results. the second calculation divides the current value of the total variable by the current input file line number (NR). It would seem that you need to be able to determine how many input lines you have in each set to get the average value of the value of the values in that set.

Why are you using substr($1, 1, 7) if you want to match the 1st 4 characters in the 1st field. (Have you changed your requirement to match the 1st 7 characters of the 1st field?)

Do you understand the difference between what you did and what you see in the following revision of my (commented) suggestion:
Code:
awk '	# Invoke awk and start the script specifying actions to be performed.
function print_total() {
	# Define function to print the total and average acccumulated for the
	# previous set of lines with the same four characters at the start of
	# the 1st field.
	printf("\n    Total Sum of %s: %d\n", last, total)
	printf("\nTotal Average of %s: %.2f\n", last, total / count)
}
last != substr($1, 1, 4) {
	# Perform the actions in this group when the 1st four characters in the
	# 1st field on this line is not the same as the 1st four characters in
	# the 1st field that we saw on the previous line.  Since there is no
	# previous line when we are reading the 1st line from our input file,
	# we also perform the actions in this group when processing the 1st
	# line in the input file.
	if(NR > 1) {
		# Perform these actions when we are not processing line #1.
		print_total()				# Print the total for
							# for the previous set.
		printf("\n=======================\n")	# Print a set separator.
	}
	# Save the 1st four characters from this line to compare against
	# subsequent input lines.
	last = substr($1, 1, 4)
	# Clear the count and total for this new set.
	count = total = 0
}
{	# Perform the actions in this group for every line in the input file.
	print		# Print the current input line.
	count++		# Increment the number of lines seen in this set.
	total += $2	# Add the contents of the 2nd field on this line to the
			# total for this set.
}
END {	# Perform the actions in this group after we have read the last line of
	# input from the input file.
	print_total()	# Print the total and average for the last set.
}' hhhh	# End the awk script and name the file to be processed.

Note that this just calculates the average when it is being printed instead of calculating averages every time a line is read and discarding that value except on the last line of a set.
This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 08-15-2018
Code:
$ cat hhhh
aaabbb1a 4000
aaabbb1g 2000
aaabbb1f 8000
baabbb4f 5000
baabbb4d 1000
baabbb4s 9000
cddbbbg1 10000
cddbbbg3 11000
cddbbbge 3000

##sh ave.sh

aaabbb1a 4000
aaabbb1g 2000
aaabbb1f 8000

Total Sum of aaabbb1 site is: 8000 Mbps

Total Average of aaabbb1 site is: 2666 Mbps

=======================
baabbb4f 5000
baabbb4d 1000
baabbb4s 9000

Total Sum of baabbb4 site is: 9000 Mbps

Total Average of baabbb4 site is: 3000 Mbps

=======================
cddbbbg1 10000
cddbbbg3 11000
cddbbbge 3000

Total Sum of cddbbbg site is: 3000 Mbps

Total Average of cddbbbg site is: 1000 Mbps
$

ill try different combination...not working right

------ Post updated at 07:27 PM ------

Never mind..thank you sir. I figured it out.. THank you for the comments..
# 10  
Old 08-17-2018
Don, my challenge now is putting date on total and averate. Putting a timestamp.. Trying every combination and googling..

------ Post updated at 11:34 AM ------

I modified it to
Code:
        printf("\n%s,%s,%d,total",  last, last, total)
        printf("\n%s,%s,%.1f,average\n",  last, last, total / count)

For parsing, but im hoping to have value like this
Code:
"date"baabbb4,baabbb4,101.95,total
"date"baabbb4,baabbb4,101.95,average

------ Post updated at 11:45 AM ------

I tried to add the line below

Code:
awk '
function print_total() {
        printf("\n%s,%s,%d,total", last, last, total)
        printf("\n%s,%s,%.1f,average\n", last, last, total / count)
        printf("%s", cmd)
}

#{ cmd = system("date") }
{ cmd = system("date  ") }

But when i put it in printf,,its throwing a lot of error..

------ Post updated at 12:05 PM ------

Code:
awk '
function print_total() {
        printf("\n%s,%s,%d,total", last, last, total)
        printf("\n%s,%s,%.1f,average\n", last, last, total / count)
 #       printf("%s", cmd)
}

#{ cmd = system("date") }

last != substr($1, 1, 7) {
        if(NR > 1) {
                print_total()
                printf("\n\n")
        }
        last = substr($1, 1, 7)
        count = total = 0
}
{
        print
        count++
        total += $2
}
{  cmd = system("date") }

END {   print_total()
}'


output:
Code:
remotese01 120.653 Mbps
Fri Aug 17 17:02:26 UTC 2018
remotese02 91.7216 Mbps
Fri Aug 17 17:02:26 UTC 2018

remotese,remotese,212,total
remotese,remotese,106.2,average

thats what i got..
im hoping of the output below

Code:
Fri Aug 17 17:02:26 UTC 2018, remotese,remotese,210,total
Fri Aug 17 17:02:26 UTC 2018, remotese,remotese,105.1,average

------ Post updated at 01:28 PM ------

I found a way.. but ugly..
revise code:
Code:
awk '
function print_total() {
        printf("\n,%s,%s,%d,total", last, last, total)
        printf("\n,%s,%s,%.1f,average\n", last, last, total / count)
 #       printf("%s", cmd)
}

#{ cmd = system("date") }

last != substr($1, 1, 7) {
        if(NR > 1) {
                print_total()
                printf("\n\n")
        }
        last = substr($1, 1, 7)
        count = total = 0
}
{
        print
        count++
        total += $2
}

END {   print_total()

}' rawdata.log |sed -e "s/.*average.*/`date`&/" -e "s/.*total.*/`date`&/" > /datarate/datarate.log

cat /datarate/datarate.log


output:
Code:
remotese01 130.326 Mbps
remotese02 99.1562 Mbps

Fri Aug 17 18:25:30 UTC 2018,remotese,remotese,229,total
Fri Aug 17 18:25:30 UTC 2018,remotese,remotese,114.7,average


server1101 219.804 Mbps
server1102 348.427 Mbps

Fri Aug 17 18:25:30 UTC 2018,server11,server11,568,total
Fri Aug 17 18:25:30 UTC 2018,server11,server11,284.1,average

------ Post updated at 01:50 PM ------

The reason that is ugly, because, i have 100 entries in my for loop..they have the same time stamp, unlike if i will embed the date within the awk,,each line will have diffrent time by seconds or minutes..

------ Post updated at 02:02 PM ------

New challenge.. New desired Output.. FInal..need help on this..

Code:
Fri Aug 17 18:25:30 UTC 2018,remotese01 130.326 Mbps
Fri Aug 17 18:25:30 UTC 2018,remotese02 99.1562 Mbps
Fri Aug 17 18:25:30 UTC 2018,remotese01,remotese,229,total
Fri Aug 17 18:25:30 UTC 2018,remotese02,remotese,114.7,average


Fri Aug 17 18:25:30 UTC 2018,server1101 219.804 Mbps
Fri Aug 17 18:25:30 UTC 2018,server1102 348.427 Mbps
Fri Aug 17 18:25:30 UTC 2018,server1101,server11,568,total
Fri Aug 17 18:25:30 UTC 2018,server1102,server11,284.1,average


Last edited by kenshinhimura; 08-17-2018 at 01:55 PM..
# 11  
Old 08-17-2018
You have a few problems. The date utility always terminates its output with a <newline>. The awk system() function sends its output to awk's standard output and returns the exit status of the command that you invoke. (It does not return the output produced by the command you invoke as a string that you can assign to a variable in awk as you tried to do in your code.)

To fix that, you can use sprintf() (in this case a couple of times) to create a date command string to give to system() that will invoke the date utility with a format string that will print the two lines of output you want each with the leading date and time field. Note that the following invokes date each time you call print_total() as in your earlier awk code:
Code:
function print_total() {
	dc = sprintf("date \"+%%a %%b %%d %%H:%%M%%S %%Z %%Y,%s-%s,%d,total%%n",
		last, last, total)
	dc = sprintf("%s%%a %%b %%d %%H:%%M%%S %%Z %%Y,%s-%s,%.1f,average\"",
		dc, last, last, total / count)
	system(dc)
}

If you just want to invoke date once at the start of your script and print that timestamp in all of the output produced by a single run of your awk script (which is closer to what you're doing now with awk and sed, but you're invoking date twice and may end up with one date and time on the total lines and a later date and time on the average lines), it would be much simpler to use:
Code:
awk -v dt="`date`" '
function print_total() {
	printf("%s,%s-%s,%d,total\n", dt, last, last, total)
	printf("%s,%s-%s,%.1f,average\n", dt, last, last, total / count)
}

PS I didn't see your last two updates before posting this. But, it should give you enough information to make changes to get what you want.

Last edited by Don Cragun; 08-17-2018 at 05:40 PM.. Reason: Add PS.
This User Gave Thanks to Don Cragun For This Post:
# 12  
Old 08-20-2018
I did different code on the first block.. Nasty Ugly. Etc. But it worked. Hoping to clean that one. More cleane. I got the output right... But with an ugly code from me. Thank you for your help. But if by chance.. Can you make my code cleaner? Especially the for loop.

Code:
for i in `cat DATASERVER`;do  echo -n $i",";ssh $i "echo -n $i,|cut -c1-7 -n; f=\`/sbin/ip a|egrep '127.127|128.128' |head -1| awk '{print \$NF }' \` ; sar -n DEV|grep \$f|grep Average|awk '{print \$5 * 8 / 1000 \" Mbps\" }'"  ; done  |sed 'N;s/\n/, /' > rawdata.log



Code:
awk '
function print_total() {
        printf("\n%s,%s,%d,total", full, last, total)
        printf("\n%s,%s,%.1f,average\n", full, last, total / count)
 #       printf("%s", cmd)
}

#{ cmd = system("date") }

last != substr($1, 1, 7) {
        if(NR > 1) {
                print_total()
                printf("\n\n")
        }
        last = substr($1, 1, 7)
        full = substr($1, 1, 9) #added
        count = total = 0
}
{
        print
        count++
        total += $2
        full # added
}

END {   print_total()

}' rawdata.log |sed -e "s/.*nc.*/`date`,&/" > /datarate/datarate.log

cat /datarate/datarate.log

this was the output:
Code:
Mon Aug 20 13:22:53 UTC 2018,datasdr01,datasdr, 91.2404 Mbps
Mon Aug 20 13:22:53 UTC 2018,datasdr02,datasdr, 64.7573 Mbps
Mon Aug 20 13:22:53 UTC 2018,datasdr03,datasdr, 67.3033 Mbps
Mon Aug 20 13:22:53 UTC 2018,datasdr04,datasdr, 64.3564 Mbps
Mon Aug 20 13:22:53 UTC 2018,datasdr05,datasdr, 64.533 Mbps
Mon Aug 20 13:22:53 UTC 2018,datasdr06,datasdr, 123.74 Mbps
Mon Aug 20 13:22:53 UTC 2018,datasdr07,datasdr, 0.43832 Mbps
Mon Aug 20 13:22:53 UTC 2018,datasdr08,datasdr, 34.9874 Mbps
Mon Aug 20 13:22:53 UTC 2018,datasdr09,datasdr, 34.2882 Mbps

Mon Aug 20 13:22:53 UTC 2018,datasdr01,datasdr,545,total
Mon Aug 20 13:22:53 UTC 2018,datasdr01,datasdr,60.6,average


Mon Aug 20 13:22:53 UTC 2018,comondr01,comondr, 292.405 Mbps
Mon Aug 20 13:22:53 UTC 2018,comondr02,comondr, 265.401 Mbps
Mon Aug 20 13:22:53 UTC 2018,comondr03,comondr, 290.693 Mbps
Mon Aug 20 13:22:53 UTC 2018,comondr04,comondr, 277.023 Mbps
Mon Aug 20 13:22:53 UTC 2018,comondr05,comondr, 263.343 Mbps
Mon Aug 20 13:22:53 UTC 2018,comondr06,comondr, 384.669 Mbps
Mon Aug 20 13:22:53 UTC 2018,comondr08,comondr, 378.82 Mbps

Mon Aug 20 13:22:53 UTC 2018,comondr01,comondr,2152,total
Mon Aug 20 13:22:53 UTC 2018,comondr01,comondr,307.5,average


Mon Aug 20 13:22:53 UTC 2018,servedr01,servedr, 54.6014 Mbps
Mon Aug 20 13:22:53 UTC 2018,servedr02,servedr, 48.7523 Mbps
Mon Aug 20 13:22:53 UTC 2018,servedr03,servedr, 48.4779 Mbps

Mon Aug 20 13:22:53 UTC 2018,servedr01,servedr,151,total
Mon Aug 20 13:22:53 UTC 2018,servedr01,servedr,50.6,average

------ Post updated at 08:51 AM ------

Anyway, your code, doesnt work on mine..

1st code, it is throwing double date..


2nd code from yours
Code:
awk: cmd. line:6: }
awk: cmd. line:6: ^ syntax error

# 13  
Old 08-20-2018
Your desired output changes every time you add a new post to this thread.

I suggested code in post #11 that gets rid of the need to need for the for loop you showed us in post #12 and produces the results that loop would produce inside the print_total() function.

The code that I suggested there worked perfectly with the earlier sample data you provided to produce the output you said you wanted in an earlier post in this thread. If you modified that code to produce a different output format and introduced a syntax error in the code as you did it, I can't help you without seeing how you modified the code. Since your requirements are constantly changing and since you haven't provided us with any sample input that could possibly produce the most recent output you're showing us, I'm not motivated to try to rewrite my suggestion again hoping to guess at what your next output format requirements might be.
# 14  
Old 08-20-2018
Output is final. Made it work. but with ugly code.
Code:
Mon Aug 20 13:22:53 UTC 2018,comondr01,comondr, 292.405 Mbps
Mon Aug 20 13:22:53 UTC 2018,comondr02,comondr, 265.401 Mbps
Mon Aug 20 13:22:53 UTC 2018,comondr03,comondr, 290.693 Mbps
Mon Aug 20 13:22:53 UTC 2018,comondr04,comondr, 277.023 Mbps
Mon Aug 20 13:22:53 UTC 2018,comondr05,comondr, 263.343 Mbps
Mon Aug 20 13:22:53 UTC 2018,comondr06,comondr, 384.669 Mbps
Mon Aug 20 13:22:53 UTC 2018,comondr08,comondr, 378.82 Mbps

Mon Aug 20 13:22:53 UTC 2018,comondr01,comondr,2152,total
Mon Aug 20 13:22:53 UTC 2018,comondr01,comondr,307.5,average


Mon Aug 20 13:22:53 UTC 2018,servedr01,servedr, 54.6014 Mbps
Mon Aug 20 13:22:53 UTC 2018,servedr02,servedr, 48.7523 Mbps
Mon Aug 20 13:22:53 UTC 2018,servedr03,servedr, 48.4779 Mbps

Mon Aug 20 13:22:53 UTC 2018,servedr01,servedr,151,total
Mon Aug 20 13:22:53 UTC 2018,servedr01,servedr,50.6,average

Because i will run the command below to all 100 servers. Then the ethernet card will vary.
Code:
# sar -n DEV|grep Average
Average:         eth0     10.15      5.18      5.87      6.21      0.00      0.00      0.00

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 to average matching lines in file

The awk below executes and is close (producing the first 4 columns in desired). However, when I add the sum of $7, I get nothing returned. Basically, I am trying to combine all the matching $4 in f1 and output them with the average of $7 in each match. Thank you :). f1 ... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. UNIX for Beginners Questions & Answers

Random letters

Hi there, first of all this is not homework...this is a new type of exercise for practicing vocabulary with my students. I have a file consisting of two columns, separated by a tab, each line consisting of a word and its definition, separated by a line break. What i need is to replace a... (15 Replies)
Discussion started by: eldeingles
15 Replies

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

4. Shell Programming and Scripting

Randomize letters

Hi, Is there a tool somewhat parallel to rev, but which randomizes instead of reverses? I've tried rl, but I can only get it to randomize words. I was hoping for something like this echo "hello" | ran leolh less simpler solutions are also welcome. Sorry if the question is... (21 Replies)
Discussion started by: jeppe83
21 Replies

5. Shell Programming and Scripting

How to add lines of a file and average them

I'm reading in numbers from a file and trying to add them together. Here is the code so far. I know the 1+2+3.... part is wrong. The file has five numbers in it with each number on its own line. The numbers are decimals if that matters. Thanks. while read EachLine do echo $EachLine done <... (6 Replies)
Discussion started by: AxlVanDamme
6 Replies

6. Shell Programming and Scripting

Perl- Finding average "frequency" of occurrence of duplicate lines

Hello, I am working with a perl script that tries to find the average "frequency" in which lines are duplicated. So far I've only managed to find the way to count how many times the lines are repeated, the code is as follows: perl -ae' my $filename= $ENV{'i'}; open (FILE, "$filename") or... (10 Replies)
Discussion started by: acsg
10 Replies

7. Shell Programming and Scripting

print running field average for a set of lines

Hi everyone, I have a program that generates logs that contains sections like this: 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 ... (3 Replies)
Discussion started by: euval
3 Replies

8. UNIX for Advanced & Expert Users

Add letters

I want to add letters A,B,C,… in front of every line of input while printing them out using PERL. eg A file is parsed as a cmd line arg and its context will be displayed as A line1... B line 2.. I tried this..but I want better and perfect solution! !perl -p my $counter; BEGIN { $counter... (4 Replies)
Discussion started by: aadi_uni
4 Replies

9. Shell Programming and Scripting

need to delete lines that start with letters

Hi, I need to remove all lines from a file that do not start with numbers For instance, if the first three characters on any line are not numbers, delete those lines I've tried to do it with awk and it's not working, any ideas ? Thanks (5 Replies)
Discussion started by: sfisk
5 Replies

10. Shell Programming and Scripting

transposing letters

Hi, I've written a shell function in bash that reads letters into an array, then outputs them in one column with: for n in "${array}"; do echo $n done I was wondering if anyone knew how i would transpose the letters that are output by the for loop. Right now my output is: aabbcc... (4 Replies)
Discussion started by: myscsa2004
4 Replies
Login or Register to Ask a Question