Skip to content

Instantly share code, notes, and snippets.

@CliveIMPISA
Created September 27, 2014 13:29
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 CliveIMPISA/b81daedc9fd7097e65ac to your computer and use it in GitHub Desktop.
Save CliveIMPISA/b81daedc9fd7097e65ac to your computer and use it in GitHub Desktop.
TSV_to_YAML.rb
#!/usr/bin/env ruby
require 'yaml'
# Only runs if and argument is provided at the command prompt
if ARGV[0]
file = File.open(ARGV[0], 'r')
linelist = []
keyarr = []
valuearr = []
flag = false
temphash = {}
file.each_line do |line|
# This reads the hash keys and stores them in a separate array.
# This block of code runs only once
if flag == false
keyarr = line.gsub("\n", '').split("\t")
flag = true
else
valuearr = line.gsub("\n", '').split("\t")
temphash = Hash[keyarr.zip valuearr]
linelist << temphash
end
end
file.close
if ARGV[1]
File.open(ARGV[1], 'w') do |files|
files.puts linelist.to_yaml
end
else
puts linelist.to_yaml
end
# Error output message if commandline argument is not provided when code is run.
else
puts 'Please enter input .tsv file'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment