【问题标题】:print lines if $2<25 from text files with sed or awk如果 $2<25 从带有 sed 或 awk 的文本文件中打印行
【发布时间】:2012-12-07 22:14:02
【问题描述】:

我想print $1 and $2 if $2&lt;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

【问题讨论】:

    标签: sed awk


    【解决方案1】:

    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 在第二个字段上排序。

    【讨论】:

      【解决方案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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-02
        • 2012-01-09
        • 2016-01-04
        • 2014-07-16
        • 2015-10-28
        • 1970-01-01
        • 2023-03-05
        • 1970-01-01
        相关资源
        最近更新 更多