parsing position files in ruby

Posted by john on Stack Overflow See other posts from Stack Overflow or by john
Published on 2010-06-01T14:15:57Z Indexed on 2010/06/01 14:33 UTC
Read the original article Hit count: 171

Filed under:
|

I have a sample position file like below.

789754654     COLORA         SOMETHING1     19370119FYY076    2342423234SS323423
742784897     COLORB        SOMETHING2      20060722FYY076    2342342342SDFSD3423

I am interested in positions 54-61 (4th column). I want to change the date to be a different format. So final outcome will be:

789754654     COLORA         SOMETHING1     01191937FYY076    2342423234SS323423
742784897     COLORB        SOMETHING2      07222006FYY076    2342342342SDFSD3423

The columns are seperated by spaces not tabs. And the final file should have exact number of spaces as the original file....only thing changing should be the date format. How can I do this? I wrote a script but it will lose the original spaces and positioning will be messed up.

file.each_line do |line|
 dob = line.split(" ")
 puts dob[3] #got the date. change its format
 5.times {  puts "**" }
end

Can anyone suggest a better strategy so that positioning in the original file remains the same?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about parsing