Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My file is as below I want to display those records of students whose percentage is above 80.

Studid    StudName     Asp.Net   DBMS     Unix
   1       Ani         75        62       80
   2       George      90        95       82
   3       Jake        45        30       40
   4       Dennie      89        92       90

so i used following code

awk '(($3+$4+$5)/3)>80 {print}' stud

it works.. But i want to assign these columns into variable and then want to display output. So i tried below code but it didn't work

awk 'total=$3+$4+$5, per=total/3, per>80 {print}' stud

any solution with variables?

share|improve this question
    
not sure why you need to assign them into variable.. awk 'NR==1 || ($3+$4+$5)/3 > 80' stud looks simple and easy... – Sundeep 12 hours ago
up vote 5 down vote accepted

You can move the logic from the rule section into an action

awk '{total=$3+$4+$5; per=total/3; if (per>80) print}' stud
   2       George      90        95       82
   4       Dennie      89        92       90

Note that this attempts to evaluate the column headers arithmetically - which "works" because in awk, non-numeric fields are treated as zero when you try to do arithmetic on them - but would cause the header line to be printed if, for example, you changed the test to per<80. Better IMHO would be to either explicitly skip the header line using a next action for the rule NR==1

awk 'NR==1 {next} {total=$3+$4+$5; per=total/3; if (per>80) print}' stud
   2       George      90        95       82
   4       Dennie      89        92       90

or, if you want the header, explicitly print it

awk 'NR==1 {print; next} {total=$3+$4+$5; per=total/3; if (per>80) print}' stud
Studid    StudName     Asp.Net   DBMS     Unix
   2       George      90        95       82
   4       Dennie      89        92       90
share|improve this answer
    
will you please specify the meaning of {print;next} statement. Actually i want to skip first line in some queries so i wrote NR>1 but it didn't work. – Dip yesterday
    
@Dip please see updated answer – steeldriver yesterday
    
You can also do some code golfing and say {total=$3+$4+$5; per=total/3} (per>80). That last part will trigger the print if it evaluates to True. – fedorqui yesterday

Try:

awk ' 
# if /^Studid/ is matched move to the next record (row) of the input text
/^Studid/ { next }
{               
    total=$3+$4+$5
    per=total/3
    if (per > 80)  
        print 
}' stud

output

   2       George      90        95       82
   4       Dennie      89        92       90
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.