【问题标题】:Search pattern between tags in htmlhtml中标签之间的搜索模式
【发布时间】:2021-04-17 14:32:32
【问题描述】:

我需要从具有特定标题的标签中获取价值。

我有这个命令。

sed -n 's/title="view quote">\(.*\)<\/a>/\1/p' index.html

这是 index.html 的一部分,我需要“生活中的一切都是运气”

    <a title="view quote" href="https://www.brainyquote.com/quotes/donald_trump_106578" class="oncl_q">
<img id="qimage_106578" src="./Donald Trump Quotes - BrainyQuote_files/donaldtrump1.jpg" class="bqphtgrid" alt="Everything in life is luck. - Donald Trump">
</a>
</div>
<a href="https://www.brainyquote.com/quotes/donald_trump_106578" class="b-qt qt_106578 oncl_q" title="view quote">Everything in life is luck.</a>
<a href="https://www.brainyquote.com/quotes/donald_trump_106578" class="bq-aut qa_106578 oncl_a" title="view author">Donald Trump</a>
</div>

我需要所有这些值来填充 bash 中的数组。

【问题讨论】:

  • mapfile -t array &lt; &lt;(printf '%s\n' 'g/^&lt;a.*title="view quote"&gt;\(.\{1,\}\)&lt;\/a&gt;/s//\1/p' Q | ed -s index.html)

标签: regex bash sed grep


【解决方案1】:

您的 sed 命令大部分都很好 - 只是在正则表达式的每一端都缺少 .* 以删除额外的头部和尾部。

此命令提取具有您特定标题的所有值:

sed -n 's/.*title="view quote">\(.*\)<\/a>.*/\1/p' index.html

放入数组:

IFS=$'\n' array=( $(sed -n 's/.*title="view quote">\(.*\)<\/a>.*/\1/p' index.html) )

验证你的结果数组:

for ((i=0;i<${#array[@]};i++)); do
    echo ${array[$i]}
done

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-04
    • 2015-09-04
    • 1970-01-01
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 2011-11-15
    相关资源
    最近更新 更多