Ruby pros: Please help...How to create multiple copies of FileA u sing names from FileB?


(Kurt Euler) #1

All-

I’d be grateful for a jumpstart on this problem:

I have two text files “FileA” and “FileB”. FileB contains many lines, each line has three colon delimted fields (field1, field2, field3). What I want to do is step through FileB line-by-line, replace some text in FileA with the values in the field2 and field3 fields of FileB, and save the modified
FileA with a file name that comes from the text in field1 of FileB.

I can’t seem to get Ruby to simply create copies of FileA named after short text in each row (that is, field1 only. No field2 or field 3 present.)

I tried using the “for line in file” statement as follows, but with no results:

file = open("controllfile", "w+")
for line in file
         
  (tried various things here but nothing worked.)

end                             
file.close                      

If you could help me create code that just creates copies of FileA using the values (for filenames) from the rows of FileB, that’d be great.

Thanks very much in advance for your help!

-Kurt Euler

···

-----Original Message-----
From: Stefan Schmiedl [mailto:s@xss.de]
Sent: Sunday, June 02, 2002 1:09 AM
To: ruby-talk@ruby-lang.org
Subject: Re: Problem: Newbie with a question on file arrays and file
insertion …

On Sun, 02 Jun 2002 07:36:57 GMT, Kurt Euler keuler@portal.com wrote:

Ruby pros:

I am a Ruby newbie, and would appreciate help with the following to jump-start a project.

1)- How can I read the contents of a text file into a two-dimentional array, such that one dimension are the rows of text and the other dimension are fields that occur on each line as delimited by some character.

 Note: A tutorial I'm using shows that I can use the readlines method 
 to create a one-dimensional array, with each line from the source file
 an element. It seems like I ought to be able to use the same or
 similar method to include the second dimension (delimited fields in 
 each row).

File(“whatever”).readlines.collect { |line|
line.split “separator”
}

comes to mind here.

2)- Say I have text files FileA and FileB. FileA contains a text tag in the somewhere in it called, say, “”. How can I:

 - locate the tag <foobar> in FileA and
 - replace <foobar> with the entire contents of FileB.

 In short, I want to insert FileB in FileA where the <foobar> tag is,
 then delete the tag.

if the files are not too large, you could read them as a whole
and then just do string substitution.

a = File(“a”).read
b = File(“b”).read

a.gsub(//, b)

Be warned: this is written on a sunday morning before breakfast :slight_smile:

s.


(Park Heesob) #2

Hi,

“Kurt Euler” keuler@portal.com wrote in message
news:C47CCC6238EFD4119C5200508B95A100074AF629@cup1ex1.portal.com

All-

I’d be grateful for a jumpstart on this problem:

I have two text files “FileA” and “FileB”. FileB contains many lines, each
line has three colon delimted fields (field1, field2, field3). What I want
to do is step through FileB line-by-line, replace some text in FileA with
the values in the field2 and field3 fields of FileB, and save the modified
FileA with a file name that comes from the text in field1 of FileB.

I think you want this code, don’t you?

content = File.new(“FileA”).read
IO.foreach(“FileB”) { |x|
field = x.chop.split(’:’)
content2 = content

modify content2 here

File.new(field[0],“w+”).write(content2)
}

Park Heesob.