Find avg using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find avg using awk
# 1  
Old 12-19-2009
Find avg using awk

Hi, i need some help plz...
The file data.txt contains: code of student,surname and name,code of lesson,grade of lesson.The number of lessons of each student is not the same.

Code:
25,Jackson Steve,12,4,34,2,65,2
29,Jordan Mary,13,6,23,8,56,4,34,2
04,Leven Kate,14,6,15,6,26,4
34,Owen Chris,85,6,39,4,42,6,65,8,12,6

I want to find the avg for lessons that the grade is >=5.
This command:
Code:
cat data.txt | awk -F, '{for (i=4;i<=NF;i+=2)if($i>=5) printf $i" ";printf"\n"}'

cats grades of students >=5.

How to find avg ? I want to cat this result:
Code:
Jordan Mary avg 7
Leven Kate avg 6
Owen Chris avg 6.6


Last edited by Franklin52; 12-19-2009 at 08:37 AM.. Reason: Please use code tags!!
# 2  
Old 12-19-2009
how does Jordan Mary's average of 7 come about?? show the calculation.
# 3  
Old 12-19-2009
contents of avg.awk

Code:
BEGIN {
        FS=","
}

{
        s = 0
        j = 0
        for (i=4; i <= NF; i +=2)
        if ($i >=5)
        {
                s += $i
                j++
        }
        if ( j == 0)
                next
        a = s / j
        if ( a >= 5 )
                print $2 " avg " a
}

results of execution:

Code:
$ awk -f avg.awk data.txt
Jordan Mary avg 7
Leven Kate avg 6
Owen Chris avg 6.5

# 4  
Old 12-19-2009
Thanks a lot for the answer !


Quote:
Originally Posted by gh0std0g74
how does Jordan Mary's average of 7 come about?? show the calculation.
I want only the grades >=5 so, for Jordan Mary is: 6+8/2 =7
# 5  
Old 12-19-2009
if you can use Python, here's an alternative
Code:
for line in open("file"):
    line=line.rstrip()
    sl=line.split(",")
    t=[]
    num=[int(i) for i in sl[3::2]]
    for n in num:
        if n>=5:
            t.append(n)
    try:
        print "Avg: %s %.2f" % ( sl[1],sum(t)//len(t))
    except : pass

output
Code:
$ ./python.py
Avg: Jordan Mary 7.00
Avg: Leven Kate 6.00
Avg: Owen Chris 6.00

# 6  
Old 12-19-2009
Quote:
Originally Posted by gh0std0g74
if you can use Python, here's an alternative
Code:
for line in open("file"):
    line=line.rstrip()
    sl=line.split(",")
    t=[]
    num=[int(i) for i in sl[3::2]]
    for n in num:
        if n>=5:
            t.append(n)
    try:
        print "Avg: %s %.2f" % ( sl[1],sum(t)//len(t))
    except : pass

output
Code:
$ ./python.py
Avg: Jordan Mary 7.00
Avg: Leven Kate 6.00
Avg: Owen Chris 6.00

Thanks, i don't use Python but i hop someday Smilie
# 7  
Old 12-19-2009
Another approach:
Code:
tr ',' '\n' < infile |
awk 'NR%2        {next}
     /^[A-Za-z]/ {n=$0;next}
     !/^[0-4]$/  {A[n]+=$1;B[n]++}
     END         {for (i in A) print i" avg "A[i]/B[i]}'

Output:
Code:
Jordan Mary avg 7
Leven Kate avg 6
Owen Chris avg 6.5

Or if your awk supports multi-character RS:
Code:
awk 'BEGIN       {RS="[,\n]"}
     NR%2        {next}
     /^[A-Za-z]/ {n=$0;next}
     !/^[0-4]$/  {A[n]+=$1;B[n]++}
     END         {for (i in A) print i" avg "A[i]/B[i]}' infile


Last edited by Scrutinizer; 12-19-2009 at 10:28 PM.. Reason: Should be >=5 instead of >5 Thanks jp
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Sybase ASE - AVG Function Error

Hi Team - I am using Sybase ASE 15.7 version. Below query is throwing an error stating Error : incorrect syntax near the keyword 'OVER' SELECT EMPLOYEE_ID , EMPLOYEE , Department, CAST( Salary as DECIMAL( 10, 2 ) ) AS Salary CAST(AVG( Salary) OVER ( PARTITION... (3 Replies)
Discussion started by: Perlbaby
3 Replies

2. Shell Programming and Scripting

Avg calculation for top 5 records

Hi, we have a file which contains Area, Country & Rank fields. AREA,COUNTRY,RANK A,MX,1 A,MX,2 A,MX,4 A,MX,6 A,MX,3 A,MX,8 A,IN,7 A,IN,5 A,IN,2 B,CN,6 B,CN,2 B,CN,7 B,CN,0 -------- -------- (1 Reply)
Discussion started by: JSKOBS
1 Replies

3. Shell Programming and Scripting

awk to find the avg of every 3 rows but only show last result?

Hi, I've got as far as this: awk '{sum+=$1}(NR%3==1){avg=sum/3; print avg}' input.txt Input it: 0.1 txt txt 0.2 txt txt 0.3 txt txt So, the I get the results: 0.0333333 0.133333 0.2 (8 Replies)
Discussion started by: JohnnyEnglish
8 Replies

4. Shell Programming and Scripting

Get the min avg and max with awk

aaa: 3 ms aaa: 2 ms aaa: 5 ms aaa: 10 ms .......... to get the 3 2 5 10 ...'s min avg and max something like min: 2 ms avg: 5 ms max: 10 ms (2 Replies)
Discussion started by: yanglei_fage
2 Replies

5. AIX

Nmon max and avg for cpu and memory

Hi All, Anyone know how to capture the nmon avg and max cpu and memory for one of the AIX server for Monthly Utilization Report purposes ? Thanks. ---------- Post updated at 05:18 AM ---------- Previous update was at 05:07 AM ---------- if possible use shell script to count or sum... (6 Replies)
Discussion started by: ckwan
6 Replies

6. Shell Programming and Scripting

Calculate avg response time on hourly basis

Hi, I am trying to calculate avg response time on hourly basis from the log file which has millions of records. As of now I am trying with creating temp file which will have lines with unique id and start time and end time and after that another script will run on this temp file to... (7 Replies)
Discussion started by: random_thoughts
7 Replies

7. Shell Programming and Scripting

Avg using awk

Coins: gold 1 1986 USA American Eagle gold 1 1908 Austria-Hungary Franz Josef 100 Korona silver 10 1981 USA ingot gold 1 1984 Switzerland ingot gold 1 1979 RSA Krugerrand gold 0.5 1981 RSA Krugerrand gold 0.1 1986 PRC Panda silver 1 1986 USA Liberty dollar gold 0.25 1986 USA Liberty... (3 Replies)
Discussion started by: Ramesh M
3 Replies

8. Shell Programming and Scripting

Extracting avg latency value from ping output

Hello Everyone, Below is the output of the ping from a router. Please help with a script which extract the Avg value from the o/p (Avg here = 4, as depicted below) and put the value into a new file. Will appreciate your help dearly Router#ping 36.36.36.36 Type escape sequence to abort.... (2 Replies)
Discussion started by: sanjugab
2 Replies

9. UNIX for Advanced & Expert Users

ps avg | grep ? filter the desired out put.

Hi Folk, Following is the command I used to get data related to the DataFlowEngine. I wanted to know the % usage of cpu and memory. ps avg | grep Data This command will show the processes with its PID as : PID TTY STAT TIME PGIN SIZE RSS LIM TSIZ TRS %CPU %MEM COMMAND ... (1 Reply)
Discussion started by: varungupta
1 Replies

10. SuSE

sles 9 - sudden high load avg

Hi Running SLES 9(4) on PE 1950. I saw yesterday that the load average on the machine was 54 and keeping around that number. Later I found there were 54 /USR/SBIN/CRON processes running in the system. I tried to kill using killall, kill -9 pid but they did not get killed. I also tried stopping... (1 Reply)
Discussion started by: upengan78
1 Replies
Login or Register to Ask a Question