shell脚本实例

grep & awk

  1. 打印匹配行的第二列

    0.304443153 281764586 cycles

    0.304443153 76724668 instructions

    0.304443153 12055101 branch-instructions

    grep cycles test.txt | awk '{print $2}' 

    281764586

  2. grep 完全匹配
    grep -w “word”
     
    1. awk计算平均值,最大值,最小值

      apuser@BJ08491PCU01:~/task/bug1145345/perf$ cat test.txt | head -10
      # started on Tue Aug 27 09:18:25 2019

      #           time             counts unit events
           0.304443153          281764586      cycles                   
           0.304443153           76724668      instructions             
           0.304443153           12055101      branch-instructions      
           0.304443153            2004530      branch-misses            
           0.304443153           22436839      cache-references         
           0.304443153            1346060      cache-misses             
           0.609641384          291220773      cycles  

      求平均值
      grep -w "branch-instructions" test.txt | awk '{print $2}'| awk '{sum+=$1}END{print "Average = ", sum/NR}'
      
      求最大值
      awk 'BEGIN {max = 0} {if ($1>max) max=$1 fi} END {print "Max=", max}'​​​​​​​
      求最小值
      awk 'BEGIN {min = 6553565} {if ($1awk '{sum+=$1} END {print "Sum = ", sum}' 

       

​​​​​​​

你可能感兴趣的:(kernel)