From: Robert Klemme Date: 2010-03-18T19:17:28+09:00 Subject: Re: Replace Text at Specific Positions Across Files 2010/3/17 Shiny Hydra : > I'm new to Ruby and after trying to look through a ton of classes and > methods, I decided it would be best to ask some more seasoned > individuals for help.  I'm currently working on a project that > essentially deals with a relational DB in text format.  There is a > standard layout throughout a variety of text files and each of them have > corresponding information.  For example, if positions 10-17 is populated > with genderM in a file with .aaa extension, male should be written in > positions 30-34 in a file with .bbb extensions.  There can be multiple > lines, each relating to a different object. So your file has fixed width records? This is important to know, otherwise approach 2 from below becomes tricky (you basically need to read line by line in order to find the proper position whereas you otherwise can calculate the position via record size). > After looking through the file and directory classes I can't find a > obvious way to code this.  How would I write/overwrite a specific > position number in a certain extension based on information from another > file?  I know to read the information I just use readlines and store the > position using something similar to textfile1[10,7] and that I can use > file.extname to get the extension, but beyond this I'm stuck.  I > apologize for the basic question, but I would greatly appreciate the > help. You have basically two options: 1. do it in memory 2. do it on disk ad 1: You can read a complete file by using IO#read into a String, then you manipulate it via String manipulation methods and write it out. This obviously only works up to a certain file size. ad 2: Use IO#seek to find the proper write position and use IO#write to overwrite bytes at this position. Btw, is there a particular reason why you create what looks like a relational database based on text files? Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/