Use of Awk filter to get the students records details in descending order of total score

大憨熊 提交于 2020-12-13 03:40:30

问题


Student details are stored in a file system as follows:

Roll_no,name,socre1,score2
101,ABC,50,55
102,XYZ,48,54
103,CWE,42,34
104,ZSE,65,72
105,FGR,31,45
106,QWE,68,45

Q.Write the unix command to display Roll_no and name of the student whose total score is greater than 100 the student details are to be displayed sorted in descending order of the total score.

total score as to be calculated as follows :-

totalscore=score1+score2

file also content the header(Roll_no,name,socre1,score2)

My solution:

awk 'BEGIN {FS=",";OFS=" "} {if(NR>1){if($3+$4>100){s[$1]=$2}}} END{for (i in s) {print i,h[i]}}' stu.txt| sort -rk 2n

I am not getting how to get sorting according to total score? please help guys!

output:-

104 ZSE
106 QWE
101 ABC
102 XYZ

回答1:


$ awk 'BEGIN {OFS=FS=","} 
       NR==1 {print $0, "total"; next} 
             {if(($5=$3+$4)>100) print | "sort -t, -k5nr"}' file

Roll_no,name,socre1,score2,total
104,ZSE,65,72,137
106,QWE,68,45,113
101,ABC,50,55,105
102,XYZ,48,54,102

without header and individual scores

$ awk 'BEGIN{OFS=FS=","} 
       NR>1 && ($3+=$4)>100{print $1,$2,$3}' file | sort -t, -k3nr

104,ZSE,137
106,QWE,113
101,ABC,105
102,XYZ,102

or

$ awk 'BEGIN{OFS=FS=","} 
       NR>1 && ($3+=$4)>100 && NF--' file | sort -t, -k3nr

104,ZSE,137
106,QWE,113
101,ABC,105
102,XYZ,102

without the final score and not comma delimited

$ awk -F, 'NR>1 && ($3+=$4)>100 && NF--' file | sort -k3nr | cut -d' ' -f1,2

104 ZSE
106 QWE
101 ABC
102 XYZ

reads as written if line number is greater than one (skip header) AND if field 3 + field 4 > 100 (assigned back to field 3) then if both conditions are satisfied decrement field count so that last field won't be printed.

sort the results based on the third field, remove the last field.




回答2:


Could you please try following. To keep it simple in calculation(1st get total of numbers for all lines which are greater than 100 Then sort it reverse order by total as per OP then print only first 2 columns by cut)

awk 'BEGIN{FS=OFS=","} $3+$4>100{print $1,$2,$3+$4}' Input_file |
sort -t, -nr -k3 | 
cut -d',' -f 1-2

OR in case you want output in space delimiter in output then try following.

awk 'BEGIN{FS=","} $3+$4>100{print $1,$2,$3+$4}' Input_file |
sort -nr -k3 | 
cut -d' ' -f 1-2

Explanation: Adding detailed explanation for above.

awk 'BEGIN{FS=OFS=","} $3+$4>100{print $1,$2,$3+$4}' Input_file |   ##Starting awk program setting FS, OFS as comma. Then checking 3rd+4th column sum is greater than 100 then printing 1st, 2nd field along with sum of 3rd and 4th field here. Now passing its output as input to next command.
sort -t, -nr -k3 |                                                  ##Sorting output with setting delimiter as comma and sorting it reverse order witg 3rd column here, sending output as input to next command.
cut -d',' -f 1-2                                                    ##Getting first 2 fields by setting delimiter comma here, to get name and roll number here.


OR

sort -t, -nr -k3 < <(awk 'BEGIN{FS=OFS=","} $3+$4>100{print $1,$2,$3+$4}' Input_file) | 
cut -d',' -f 1-2

OR in case you need output as space delimited then try following.

sort -nr -k3 < <(awk 'BEGIN{FS=","} $3+$4>100{print $1,$2,$3+$4}' Input_file) | 
cut -d' ' -f 1-2



回答3:


you were close:

awk 'BEGIN {FS=OFS=","} {if(NR>1){if($3+$4>100){s[$1]=$2}}} END{for (i in s) {print i,s[i]}}' stu.txt| sort -rk 2n


来源:https://stackoverflow.com/questions/62219181/use-of-awk-filter-to-get-the-students-records-details-in-descending-order-of-tot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!