parsing position files in ruby
- by john
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?