How do I get a selection from the output of a grep

I have the following text in a file :

<img id="img_1" style="display: none" src="Logs/P2P2014-04-10_14-24-49.txt"/></span></div></div><script type="text/javascript">document.getElementById('duration').innerHTML = "Finished in <strong>1m31.846s seconds</strong>";</script><script type="text/javascript">document.getElementById('totals').innerHTML = "1

What I want to do is obtain the stuff after the src i.e. Logs/P2P2014-04-10_14-24-49.txt. I tried the following and put it into a variable in ruby or so :

I tried doing :

text = `grep 'Logs\/.*txt\"'`

But that returns the entire damn line instead of only the text. How do I get this done?

  • Calling another Gem as a program from my Gem
  • How does MacPorts install packages? How can I activate a Ruby installation done via MacPorts?
  • Separate a row of strings into separate rows
  • Is there a way to glob a directory in Ruby but exclude certain directories?
  • Install Latest Stable Version of Ruby Using rbenv
  • Ruby: 'bundle exec' throws error for all shell commands
  • Ruby gems for user interaction in the shell?
  • Is there a way to call ruby1.9 without loading rubygems?
  • 3 Solutions collect form web for “How do I get a selection from the output of a grep”

    Try to use

    text=$(grep -o 'Logs\/.*txt\"')
    

    It should return only matching part of the line.

    Using Nokogiri, see how easy to solve the problem :

    require 'nokogiri'
    
    doc = Nokogiri::HTML.parse <<-html
    <img id="img_1" style="display: none" src="Logs/P2P2014-04-10_14-24-49.txt"/></span></div></div>
    html
    
    doc.at('#img_1')['src'] # => "Logs/P2P2014-04-10_14-24-49.txt"
    

    Read tutorials to understand and learn Nokogiri.

    Using sed

    sed -n 's/.*src="\([^"]*\)".*/\1/p' file
    

    Using gnu grep if support -P option

    grep -Po '(?<=src=")[^"]*' file
    
    Ruby is the best programming language in the world - Ruby on Rails.