ruby 分析日志,提取特定记录

 

读取日志中的每一行,分析后存入hash,然后做累加

adx_openx=Hash.new(0) 
File.open('watch.log.2016-08-24-21').each do |line|
  line.split("\t").each  do |elem|
    if elem.include? "keywordds"
        key = elem.split(":")[0]
        val = elem.split(":")[1]
    adx_openx[key] = val.to_i  + adx_openx[key] 
     end
 end 
end


adx_openx.sort_by {|_key, value| value}.to_h

adx_openx.keys.each  do |key|
puts " #{key}   \t   #{adx_openx[key]}   "

end 

 

posted on 2016-08-25 12:59  iokde.com  阅读(409)  评论(0编辑  收藏  举报

导航