Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #4260

Re: gsub and multiple-replacement

From Greg Hacke <greghacke@gmail.com>
Newsgroups comp.lang.ruby
Subject Re: gsub and multiple-replacement
Date 2011-05-11 08:45 -0500
Organization Service de news de lacave.net
Message-ID <a34182cf60781b8e287e7849ea3c26cb@ruby-forum.com> (permalink)
References <eb286679b6d688c796525a6786d4394f@ruby-forum.com>

Show all headers | View raw


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:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" 
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>PayloadContent</key>
  <array>
    <dict>
      <key>EmailAddress</key>
      <string>{username}</string>
      <key>Host</key>
      <string>exchange.domain.com</string>
      <key>MailNumberOfPastDaysToSync</key>
      <integer>3</integer>
      <key>Password</key>
      <string>{password}</string>
      <key>PayloadDescription</key>
      <string>Configures device for use with Microsoft Exchange 
ActiveSync services.</string>
      <key>PayloadDisplayName</key>
      <string>Exchange</string>
      <key>PayloadIdentifier</key>
      <string>com.domain.enterprise.eas</string>
      <key>PayloadOrganization</key>
      <string>TipsHouse</string>
      <key>PayloadType</key>
      <string>com.apple.eas.account</string>
      <key>PayloadUUID</key>
      <string>FE477C13-BF4F-4EA0-BE09-3968EC40C952</string>
      <key>PayloadVersion</key>
      <integer>1</integer>
      <key>SSL</key>
      <true/>
      <key>UserName</key>
      <string>domain\{userroot}</string>
    </dict>
  </array>
  <key>PayloadDescription</key>
  <string>Profile Test</string>
  <key>PayloadDisplayName</key>
  <string>TipsHouse</string>
  <key>PayloadIdentifier</key>
  <string>com.domain.enterprise</string>
  <key>PayloadOrganization</key>
  <string>Domain</string>
  <key>PayloadRemovalDisallowed</key>
  <false/>
  <key>PayloadType</key>
  <string>Configuration</string>
  <key>PayloadUUID</key>
  <string>A6B7D66D-1179-4E85-A005-4DAACD4EDF0F</string>
  <key>PayloadVersion</key>
  <integer>1</integer>
</dict>
</plist>

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.)
<dict>
      <key>EmailAddress</key>
      <string>greghacke@domain.com</string>
      <key>Host</key>
      <string>exchange.domain.com</string>
      <key>MailNumberOfPastDaysToSync</key>
      <integer>3</integer>
      <key>Password</key>
      <string>MyPassword</string>
      <key>PayloadDescription</key>
      <string>Configures device for use with Microsoft Exchange 
ActiveSync services.</string>
      <key>PayloadDisplayName</key>
      <string>Exchange</string>
      <key>PayloadIdentifier</key>
      <string>com.domain.enterprise.eas</string>
      <key>PayloadOrganization</key>
      <string>TipsHouse</string>
      <key>PayloadType</key>
      <string>com.apple.eas.account</string>
      <key>PayloadUUID</key>
      <string>FE477C13-BF4F-4EA0-BE09-3968EC40C952</string>
      <key>PayloadVersion</key>
      <integer>1</integer>
      <key>SSL</key>
      <true/>
      <key>UserName</key>
      <string>domain\greghacke</string>
    </dict>

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)}

-- 
Posted via http://www.ruby-forum.com/.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

gsub and multiple-replacement Greg Hacke <greghacke@gmail.com> - 2011-05-10 16:16 -0500
  Re: gsub and multiple-replacement 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-10 16:25 -0500
  Re: gsub and multiple-replacement 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-10 16:43 -0500
    Re: gsub and multiple-replacement Greg Hacke <greghacke@gmail.com> - 2011-05-10 19:49 -0500
      Re: gsub and multiple-replacement 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-10 21:07 -0500
      Re: gsub and multiple-replacement Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-05-11 03:00 -0500
  Re: gsub and multiple-replacement Phillip Gawlowski <cmdjackryan@googlemail.com> - 2011-05-10 16:28 -0500
  Re: gsub and multiple-replacement Greg Hacke <greghacke@gmail.com> - 2011-05-11 08:45 -0500
    Re: gsub and multiple-replacement Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-05-11 10:01 -0500
  Re: gsub and multiple-replacement 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-11 18:31 -0500
  Re: gsub and multiple-replacement 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-11 18:59 -0500

csiph-web