简体   繁体   中英

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 ie 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?

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.

Try to use

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

It should return only matching part of the line.

Using sed

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

Using gnu grep if support -P option

grep -Po '(?<=src=")[^"]*' file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM