Sponsored Content
Top Forums Shell Programming and Scripting Get the average of lines with the same first 4 letters Post 303021901 by kenshinhimura on Monday 20th of August 2018 09:51:44 AM
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

 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
All times are GMT -4. The time now is 08:42 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy