Skip to content

Instantly share code, notes, and snippets.

@GusGA
Last active August 29, 2015 14:01
Show Gist options
  • Save GusGA/6138760073a39c5f2175 to your computer and use it in GitHub Desktop.
Save GusGA/6138760073a39c5f2175 to your computer and use it in GitHub Desktop.
nedilio code
@textfile = ARGV.first
#Mi codigo bonito
def read_file_and_write_pairs(filename)
read_file = File.open(Dir.glob(File.join("**",filename)).first, 'r' )
temp_file = File.open("temp.txt", 'w' )
cdp_string = ""
read_file.readlines.each do |line|
#si linea tiene CDP guardar variable (no se como guardar en una variable que pueda usar luego)
#dentro de la iteracion declaras dos variables
if line.match(/CDP/)
cdp_string = ""
a = []
line.split.each_with_index { |it, id | a << it if id % 2 != 0 }
a.pop
a.each { |n| cdp_string << n + ' ' }
else
line.strip.split(/\s+/).each_slice(2).each do |pair|
temp_file.write(cdp_string + "#{pair.first} #{pair.last}") #imprimir con los datos de cdp
temp_file.write("\n")
end
end
end
File.rename(temp_file.path,read_file.path)
end
def concat_all_files(filename)
# Busca la unica carpeta que debe existir con todos los archivos de texto
directory = Dir.glob("**").select{|f| File.directory? f}
cdp_files = Dir.glob(directory.last+"/*")
cdp_files.each do |file|
system("sed 1,10d '#{file}' > '#{file + "temp"}' && mv '#{file + "temp"}' '#{file}'")
end
#executa los comando de concatenamiento y eliminacion de las linas inservibles
system("cat '#{directory.last}'/* > '#{filename}'")
#ejecuta la función que arregla el archivo
read_file_and_write_pairs(filename)
end
def init
concat_all_files(@textfile)
end
init
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment