From: Dan George Date: 2007-09-19T22:10:06+09:00 Subject: YAML & readlines & modify text files Hello, Can anyone help me with the following issue: I have a YAML file that looks like this: --- gm: Name01 gs: N01 --- gm: Name02 gs: N02 In ruby I'm trying to read all *.txt files in the current folder and all sub-folders. The text files look like this: Name-line: Name01 Link-line: blabla.something.N01&=bla Name-line: Name02 Link-line: blabla.something.N02&=bla My ruby code looks like this: require 'yaml' lnk = 'blabla.something.' op = '&=bla' name = File.open('name.yaml') yp = YAML::load_documents(name) do |name| txt_files = Dir.glob('**/*.txt').each do |path| file = File.open(path).readlines.each { |line| if line.match(/Link-line/) then line.gsub!(/Link-line.*/, 'Link-line: '+ lnk + name['gs'] + op) end } File.open(path, 'w'){|f| f.write file} end end My problem is that the code replaces the YAML value 'gs' with the last value found in the *.txt values. I want it to read the Name-line in each file and after that use the appropriate 'gs' value from the YAML file in "line.gsub!(/Link- line.*/, 'Link-line: '+ lnk + name['gs'] + op)" with the Link-line field. I've been trying to find a way for some time now but I just can't seem to be able to do it and I'm starting to have headaches :) so if anyone has any ideas or improvements or critiques please don't hesitate to reply. Cheers :)