From: "Jesús Gabriel y Galán" Date: 2011-05-12T00:01:27+09:00 Subject: Re: gsub and multiple-replacement On Wed, May 11, 2011 at 3:45 PM, Greg Hacke wrote: > 7stud -- wrote in post #997885: >> >> Yeah, what is so hard about posting a sample of the original file, as >> well as the modified file that you want to end up with, and leaving out >> all your confusing descriptions? > > OK, here's the inbound: > > "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> > > >  PayloadContent >   >     >      EmailAddress >      {username} >      Host >      exchange.domain.com >      MailNumberOfPastDaysToSync >      3 >      Password >      {password} >      PayloadDescription >      Configures device for use with Microsoft Exchange > ActiveSync services. >      PayloadDisplayName >      Exchange >      PayloadIdentifier >      com.domain.enterprise.eas >      PayloadOrganization >      TipsHouse >      PayloadType >      com.apple.eas.account >      PayloadUUID >      FE477C13-BF4F-4EA0-BE09-3968EC40C952 >      PayloadVersion >      1 >      SSL >       >      UserName >      domain\{userroot} >     >   >  PayloadDescription >  Profile Test >  PayloadDisplayName >  TipsHouse >  PayloadIdentifier >  com.domain.enterprise >  PayloadOrganization >  Domain >  PayloadRemovalDisallowed >   >  PayloadType >  Configuration >  PayloadUUID >  A6B7D66D-1179-4E85-A005-4DAACD4EDF0F >  PayloadVersion >  1 > > > > And the outbound replaces elements in only the array area (in this case, > just the EAS but other options may be included - LDAP, IMAP, etc.) > >      EmailAddress >      greghacke@domain.com >      Host >      exchange.domain.com >      MailNumberOfPastDaysToSync >      3 >      Password >      MyPassword >      PayloadDescription >      Configures device for use with Microsoft Exchange > ActiveSync services. >      PayloadDisplayName >      Exchange >      PayloadIdentifier >      com.domain.enterprise.eas >      PayloadOrganization >      TipsHouse >      PayloadType >      com.apple.eas.account >      PayloadUUID >      FE477C13-BF4F-4EA0-BE09-3968EC40C952 >      PayloadVersion >      1 >      SSL >       >      UserName >      domain\greghacke >     > > Now, the initial file is provided to me may be replaced as needed. I > would like to avoid converting it to ERb as I cannot gaurentee anyone > else will do the work to ensure it stays updated and correct. > > I've dug around - really - and found two examples for single element > replacement. > > My belief is that there are a plethora of options execute this. I would > like to do something like: > File.copy(master,user_file) > File.open('/tmp/temp_file.txt', 'w+') do | new_file | >     new_file.puts(File.open(user_file, 'r') do | original_file | >     original_file.read.gsub(/\{username\}/, username) >     end) > end > FileUtils.mv("/tmp/replaceable2.txt", user_file) > >  text= File.read user_file >  File.open(user_file, 'w+'){|f| f << text.gsub(/\{username\}/, > username)} You can use the block form of gsub to do only one pass: variables = {"username" => "Bob", "password" => "pwd"} file_contents = File.read("master_file").read file_contents.gsub!(/\{(.*?)\}/) {|m| variables[$1]} #and then write the file contents to another file, delete and rename Jesus.