读取日志中的每一行,分析后存入hash,然后做累加
adx_openx=Hash.new(0) File.open('watch.log.2016-08-24-21').each do |line| line.split(" ").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} #{adx_openx[key]} " end