Home > Ruby > 使用Ruby分析文件&去重

使用Ruby分析文件&去重

需要分析一个18M的文件,其中内容类似如下:

2009-06-20 00:00:07,678 [5409241381(T,105101,**,N,**,18ms)(12345678901234567890,dummy,**,888888,-,1000.00,**,-,005004-**,5409241381,0,null)]

文件以=== All done!!! ===结束。

全部日志加起来有7W多条,由于是时间程序批量请求的,所以其中肯定有很多重复的数据。我需要的只是那个12345678901234567890的20位数字而已。
so,尝试一下Ruby的脚本能力。

  file = File.open(result, 'r') # 读取文件
 
# 使用正则表达式匹配20个数字
regex = /\d{20}/
 
$array = Array.new();
 
  while(line = file.gets)  # 读取文件
    $array.push regex.match(line).to_s
    break if line == '=== All done!!! ==='
  end
  file.close
 
# 去重
$resultFile.puts($array.uniq)
puts 'convert ends!!'
Categories: Ruby Tags: , , ,
  1. No comments yet.
  1. No trackbacks yet.

Powered by WP Hashcash

Spam Protection by WP-SpamFree

cashplk的心路历程 is Digg proof thanks to caching by WP Super Cache