Created
September 27, 2014 13:29
-
-
Save CliveIMPISA/b81daedc9fd7097e65ac to your computer and use it in GitHub Desktop.
TSV_to_YAML.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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