当前位置:首页 > 开发 > 操作系统 > 正文

文件编辑 awk sed 应用小记

发表于: 2014-07-07   作者:bingtang5   来源:转载   浏览:
摘要: 早就LInux两个巨强大的文件编辑工具 sed awk命令 之前只是很肤浅的简单了解下 今天统计测试数据的时候突然想应用一下  之前是先粘贴到txt,再导入excel 统计的,很麻烦   log是类似下面的类型,统计平均时间 Starting download file 7 Starting download file 9 Starting download f

早就LInux两个巨强大的文件编辑工具 sed awk命令

之前只是很肤浅的简单了解下

今天统计测试数据的时候突然想应用一下

 之前是先粘贴到txt,再导入excel 统计的,很麻烦

 

log是类似下面的类型,统计平均时间

Starting download file 7
Starting download file 9
Starting download file 8
Download has benn done@@taskID = 1loopTimes =0   using total time ==956519
Starting download file 1
Download has benn done@@taskID = 3loopTimes =0   using total time ==987816
Starting download file 3
Download has benn done@@taskID = 9loopTimes =0   using total time ==1014928
Starting download file 9
Download has benn done@@taskID = 6loopTimes =0   using total time ==1054772
Starting download file 6
Download has benn done@@taskID = 7loopTimes =0   using total time ==1136218
Starting download file 7
Download has benn done@@taskID = 2loopTimes =0   using total time ==1167879
Starting download file 2
Download has benn done@@taskID = 0loopTimes =0   using total time ==1168782
Starting download file 0
Download has benn done@@taskID = 8loopTimes =0   using total time ==1211500
Starting download file 8
Download has benn done@@taskID = 4loopTimes =0   using total time ==1248124
Starting download file 4
Download has benn done@@taskID = 5loopTimes =0   using total time ==1340375

 

 

先用sed把带有时间标记的行筛选出来

sed -n '/done@@taskID/p' 301 >103

 

然后awk分理出最后时间的字段

awk '{print $11}' 103 >222

 

发现统计出来的带有两个等号

==1230716
==1081027
==1042317

 

继续用sed替换掉

sed 's/==//g' 222 >444

 

好了现在都是我想要的数据了

怎么直接算出他的平均值呢,或者求和

 

awk编程强大在此

awk '{sum+=$1} END {print "Sum=",sum}' 444

 

接下来计算平均值

awk '{sum+=$1} END {print "Average=",sum/NR}' 444

 

搞定!

果然强大!

文件编辑 awk sed 应用小记

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号