From: Bary Buz Date: 2009-02-25T20:30:14+09:00 Subject: changing the format of a text file Hello everyone, i am new to ruby and im having some problems trying to reformat a text file. Basically, i have a large log file which is around 200mb in the following format: ---------------------------------------------------------- 1000000 name Status :A Basetype :2 Version :1.0 | | (more fields) | Name :/file/name/etc 1000001 name Status :B Basetype :2 Version :a20 | | Name :/file/name/etc 1000002 name Status :C | ... and so on so for each 200mb file there are lot of entries. What i want to do is to open the file, read the data into an array, reformat the text and save it into another file with the following output: id, Status, Basetype, .... , Name 1000000, A, 2, ..... , /file/name/etc 1000001, B, 2, ..... , /file/name/etc i tried to write a script in ruby to do that task but i dont get any output so far. def getfile(file_name) entry = [] IO.foreach(file_name) do |fl| if fl.include? 'name' entry.push fl.scan(/\d+/)[0] elsif fl.strip =~ /\A\d/ end end entry end def writefile(file, *linedata) linedata.each do |line| file << line.join(", ") +\n" end end def readfile(file, outputfile) out = File.new(outputfile, "w+") info = [] wline = ['id', 'Status', 'Basetype', .... 'Name'] IO.foreach(file) { |line| if line =~ // wline[0]= line.scan(/\d+/) elsif line =~ /Status/ wline[1]= line.split(":")[1].scan(/[a-zA-Z]+/).join("") elsif line =~ /Basetype/ wline[2]= line.split(":")[1].scan(/\d+/).join("") | | | wline all fields | writefile(out, wline) end out.close end readfile('filename', 'outputfile') this is what ive done so far, can someone tell me whats wrong and i dont get any output at all.. Thanks in advance -- Posted via http://www.ruby-forum.com/.