Skip to content

Instantly share code, notes, and snippets.

@carlosveucv
Created May 23, 2019 21:59
Show Gist options
  • Save carlosveucv/f9c05add763019b69770ad9ec95c2e9b to your computer and use it in GitHub Desktop.
Save carlosveucv/f9c05add763019b69770ad9ec95c2e9b to your computer and use it in GitHub Desktop.
Parse all files in a folder
def main
files = Dir.entries(".").sort
files.each do |file|
array = convert_file_to_sanitized_array(file)
array_for_file(file, array) unless array.empty?
end
end
def convert_file_to_sanitized_array(file)
array = []
File.open(file) do |fp|
fp.each do |line|
array << sanitize_line(line.strip) if valid_line?(line)
end
end if File.file?(file)
array
end
def sanitize_line(line)
if valid_line?(line)
line = line.gsub('{', '').gsub('"', '').gsub('ledger_entry_guid=>', '').gsub('=>', ': ')
return line
end
nil
end
def valid_line?(line)
trace = %w(sidx sord page records ledger_entry_guid)
trace.each do |word|
return true if line.include?(word)
end
false
end
def array_for_file(file, array_to_write)
puts "\n\n#{file}"
array_to_write.each do |line|
puts line
end
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment