#!/usr/bin/env ruby if ARGV.size < 1 then puts "#{__FILE__} [options] (log.dat)" exit end =begin optionsリスト ignore <= 無視するヘッダ only <= このヘッダだけ出力 out <= 出力先、-は標準出力 remove_head <= ヘッダを取り除いて出力 =end $options = { :ignore => "", } ARGV.reject!{|arg| if arg =~ /--([^=]+)=?/ then key = $1.to_sym val = $' || true case key when :ignore then $options[key] << val.upcase else $options[key] = val end true else false end } in_fname = ARGV.shift base_fname = $options[:out] \ || (in_fname.sub(/((?:(?!\.dat).)+)(?:\.dat)?$/){$1} rescue File::basename(__FILE__, '.*')) $stderr.puts "#{in_fname || '(stdin)'} => #{base_fname == '-' ? '(stdout)' : base_fname}" class DummyIO def write(str); end def close; end end out_files = {} read_raw = proc{|io| remove_head = $options[:remove_head] while page = io.read(32) if !out_files.include?(page[0]) then if ($options[:only] && (!$options[:only].include?(page[0]))) \ || $options[:ignore].include?(page[0]) then out_files[page[0]] = DummyIO::new elsif base_fname == '-' then out_files[page[0]] = $stdout else out_fname = "#{base_fname}_#{page[0]}.dat" $stderr.puts "OUT: #{out_fname}" out_files[page[0]] = File::open(out_fname, 'w+') end end if remove_head then out_files[page[0]].write page[1..-1] else out_files[page[0]].write page end end } unless in_fname then read_raw.call($stdin) else open(in_fname, 'r'){|io| read_raw.call(io) } end out_files.each{|k, v| v.close if v != $stdout }