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


Groups > comp.lang.ruby > #4798

replace lines in a file using hash key-value pairs

From Johan Martinez <jmartiee@gmail.com>
Newsgroups comp.lang.ruby
Subject replace lines in a file using hash key-value pairs
Date 2011-05-20 02:12 -0500
Organization Service de news de lacave.net
Message-ID <235a8d6f428573541046a02f84b60764@ruby-forum.com> (permalink)

Show all headers | View raw


I want to replace matching lines in a file (config.ini) based on a hash
keys. The hash is returned by a yaml file here. Following is a code
snippet that I have written. For each key-value pair in hash I want to
go through all lines of a file and make string substitution for a
matching line. However, this code is not working and I noticed that
configfile.each method is executed only once.  I appreciate any help on
what is wrong with this code.

<pre>

  configfilename = "configuration.ini"
  puts configfilename

  # deployment properties object
  depprops = DeployProperties.new("deploy.yaml")

  # read config file
  basedir = depprops.get_download["basedir"]

  configfile = File.open("#{configfilename}","r+")

  # read filelocations hash - for each key, replace with value
  depprops.get_filelocations.each_pair do
    |key,value|
    puts " #{key} #{value}"
    configfile.each do
      |line|
      puts line
      # puts line if line.match("#{key}")
      # line.sub!("/#{key}/","#{key} = #{value}")
    end
  end

</pre>

Also, is there any other way to implement search search-replace for all
matching lines in a files based on hash/yaml file.

Thanks
jM.

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

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


Thread

replace lines in a file using hash key-value pairs Johan Martinez <jmartiee@gmail.com> - 2011-05-20 02:12 -0500
  Re: replace lines in a file using hash key-value pairs Johannes Held <johannes.held@informatik.uni-erlangen.de> - 2011-05-20 09:37 +0200
  Re: replace lines in a file using hash key-value pairs Brian Candler <b.candler@pobox.com> - 2011-05-20 03:03 -0500
  Re: replace lines in a file using hash key-value pairs Johan Martinez <jmartiee@gmail.com> - 2011-05-21 22:07 -0500

csiph-web