0

我想print $1 and $2 if $2<25从文本文件中。我还需要从所有文件中获取分数低于 25 的学生总数。如何使用 awk 或 sed 执行此操作?

students  marks
jerry     12
peter     35
john      5
jerry     15
john      10

期望的输出

jerry    12
john      5 
jerry     15
john      10

Total no:of students :- 4
4

2 回答 2

3

awk

$ awk '$2<25 {print; i++} END{print "\nTotal number of students:- "i}' file

输出:

jerry     12
john      5
jerry     15
john      10

Total number of students:- 4

如果您希望按等级(从最低到最高)排序输出:

$ sort -n -k2,2 file | awk '$2<25 {print; i++} END{print "\nTotal number of students:- "i}' 

排序输出:

john      5
john      10
jerry     12
jerry     15

Total number of students:- 4

-n数字排序; -k2,2在第二个字段上排序。

于 2012-12-07T08:36:52.477 回答
2
awk '$2<25{count++ ; print}END{print "Total No of Students :-",count}' your_file

测试如下:

> awk '$2<25{count++ ; print}END{print "Total No of Students :-",count}' temp
jerry     12
john      5
jerry     15
john      10
Total No of Students :- 4
于 2012-12-07T08:41:30.493 回答