Skip to content

Instantly share code, notes, and snippets.

@yuzutas0
Last active May 28, 2019 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuzutas0/be58c3be4843217a95bf9290be8882a6 to your computer and use it in GitHub Desktop.
Save yuzutas0/be58c3be4843217a95bf9290be8882a6 to your computer and use it in GitHub Desktop.
BQへのデータロードが失敗したからCSVファイルの該当レコードを読もうとするが、ファイルが巨大すぎてiTermが落ちたときに私は思ったんだ。該当レコードとその前後だけを抽出するスクリプトがあると便利じゃないか? ーー そう、それがすべての始まり。だからこそ、今ここにいる。僕の言葉ではない、これは僕達の言葉。 ーー 完。
# ruby big_csv_print_debug.rb input_file=test.csv output_file=result.csv line_number=10
arg = {}
ARGV.each do |s|
k, v = s.split('=')
arg[k] = v
end
p arg
target_lines = []
target_line = arg['line_number'].to_i
(-2..2).each { |num| target_lines << target_line + num }
p target_lines
line_count = 0
File.open(arg['input_file']) { |file| file.each_line { |_line| line_count += 1 } }
p line_count
result = ""
File.open(arg['input_file']) do |file|
file.each_line.with_index(1) do |line, i|
break if target_lines[-1] < i
next unless target_lines.include?(i)
result << line
end
end
File.write(arg['output_file'], result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment