[英]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
. 我想要做的是获取src之后的东西,即
Logs/P2P2014-04-10_14-24-49.txt
。 I tried the following and put it into a variable in ruby or so : 我尝试了以下内容并将其放入ruby中的变量中:
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 : 使用
Nokogiri
,看看解决问题有多容易:
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. 阅读教程以了解和学习Nokogiri。
Try to use 尝试使用
text=$(grep -o 'Logs\/.*txt\"')
It should return only matching part of the line. 它应该只返回该行的匹配部分。
Using sed 使用sed
sed -n 's/.*src="\([^"]*\)".*/\1/p' file
Using gnu grep if support -P option 如果支持-P选项,使用gnu grep
grep -Po '(?<=src=")[^"]*' file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.