title: Grep element from variable?
body: <p>I need to extract an element using a regular expression.</p>
  
  <p>First I'm reading a file from a function, then from a second function I'm reading another file and extract a value.</p>
  
  <p>When processing the read of the second function, I grep the value from the first file:</p>
  
  <pre><code>#function read file
  def get_file_as_string(filename)
    data = ''
    f = File.open(filename, "r") 
    f.each_line do |line|
      data += line
    end
    return data
  end
  </code></pre>
  
  <p>Main:</p>
  
  <pre><code>file.each do |filename|
    lunsrvfile = 'server_lun.csv'
    ldev = get_file_as_string lunsrvfile
    f=File.read(filename)
    lunid = f.scan(/^"No.","time","(\h+:\h+:\h+).*/).flatten  #it's ok:  ex: 00:02:00
         if (ldev =~ /#{lunid}/) then
             server = ldev.lines.grep(lunid) #--&gt; don't work
             #server = %x[grep lunid 'server_lun.csv' |awk -F ";" '{print $1}'] #--&gt; don't work
             puts filename
             puts lunid
             puts server
      end
  end
  </code></pre>
  
  <p>server_lun.csv:</p>
  
  <pre><code>server1;00:02:00
  server2;00:03:25
  server3;00:03:26
  </code></pre>
  
  <h1>Thx for your answer:</h1>
  
  <p>yes, i aldready test it, but when i put </p>
  
  <pre><code>server = ldev.lines.grep(/#{lunid}/)
  </code></pre>
  
  <p>all the file(lunsrvfile) is listed.</p>
  
  <p>If match i need the first value: server1 for exemple</p>
  
  <h1>Thx for your answer2:</h1>
  
  <p>but with:</p>
  
  <pre><code>server = ldev.grep(/#{lunid}/).first.split(';').first
  </code></pre>
  
  <p>Only the first server1 is print.</p>
  
  <p>output:</p>
  
  <pre><code>filename1
  lunid1
  server1
  
  filename2
  lunid2
  server1
  (...)
  </code></pre>