Comment detail

続・ファイル内の重複行削除 (Nested Flatten)
外部コマンドsortを使ってます。
・対象ファイルの各行のハッシュ値をテンポラリファイルに出力
・テンポラリファイルを順ソート後、読み込んで重複行数をカウント
・対象ファイルを再読込、重複行に出会うたびに重複行数カウントをデクリメント
100万行、60MBの数字のみファイルで、実行時間は50秒、メモリ35MB使用です。
マシンはCelleon2.8Ghz、メモリ1GB、Ubuntu7.04。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
require 'tempfile'

def make_big_seq_file(cntmax,lthmin,eps)
  cntmax.times {
    s = ""
    (lthmin+rand(eps)).times {
      s << rand(10).to_s
    }
    puts s
  }
end

#make_big_seq_file(10**6,50,20)

def double_line_reject(f)
  h = Hash.new(0)

  temp = Tempfile::new("foobar")
  open(f) {|fin|
    while line = fin.gets
      temp.puts(line.hash)
    end
    temp.close

    f = open("|sort #{temp.path}")
    iold = ""
    f.each {|i|
      i = i.chomp.to_i
      h[i] += 1 if iold == i
      iold = i
    }
    fin.rewind
    while line = fin.gets
      puts line if h[line.hash] < 1
      h[line.hash] -= 1
    end
  }
end

double_line_reject(ARGV[0])

Index

Feed

Other

Link

Pathtraq

loading...