我有一个简单的 bash 脚本。它从文件中读取一堆 HTMLtest.html
并拉出一个子字符串,但现在它没有输出任何东西。
grep yt-lockup-content test.html | while read -r line; do
datasession=`expr "$line" : ".*data-sessionlink=\"\(.*\)\" >.*"`;
users=`expr "$line" : ".*data-sessionlink=\"$datasession\" >\(.*\)</a>.*"`; echo ${users}
done
我已经指出问题在于它不适用于变量$datasession
。如果我用第一个 HTML 块中的正确文本替换它,它就可以正常工作。
grep yt-lockup-content test.html | while read -r line; do
datasession=`expr "$line" : ".*data-sessionlink=\"\(.*\)\" >.*"`;
users=`expr "$line" : ".*data-sessionlink=\"itct=CJgBEJQ1GAMiEwjOnsu1ou_RAhVJdaoKHVzWA1Eojh4\" >\(.*\)</a>.*"`; echo ${users}
done
关于如何让我的想法发挥作用的任何建议?
test.html
:
</div><div class="yt-lockup-content"><h3 class="yt-lockup-title polymer-title"><a href="/watch?v=dybBD1hbmF4" class=" yt-ui-ellipsis yt-ui-ellipsis-2 polymer-title-link yt-uix-sessionlink spf-link " data-sessionlink="itct=CJgBEJQ1GAMiEwjOnsu1ou_RAhVJdaoKHVzWA1Eojh4yCmctaGlnaC10cnZaD0ZFd2hhdF90b193YXRjaA" title="Ellen's Comments on the Travel Ban" aria-describedby="description-id-183985" dir="ltr">Ellen's Comments on the Travel Ban</a><span class="accessible-description" id="description-id-183985"> - Duration: 2:44.</span></h3><div class="yt-lockup-byline polymer-byline"><a href="/user/TheEllenShow" class="g-hovercard yt-uix-sessionlink spf-link " data-ytid="UCp0hYYBW6IMayGgR-WeoCvQ" data-sessionlink="itct=CJgBEJQ1GAMiEwjOnsu1ou_RAhVJdaoKHVzWA1Eojh4" >TheEllenShow</a> <span class="yt-uix-tooltip yt-channel-title-icon-verified yt-sprite" title="Verified"></span></div><div class="yt-lockup-meta polymer-metadata"><ul class="yt-lockup-meta-info"><li>855,335 views</li><li>1 day ago</li></ul></div></div></div></div></li><li class="yt-shelf-grid-item yt-uix-shelfslider-item"><div class="yt-lockup yt-lockup-grid yt-lockup-video vve-check clearfix" data-context-item-id="sAwFLtbc7_c" data-visibility-tracking="CJcBEJQ1GAQiEwjOnsu1ou_RAhVJdaoKHVzWA1Eojh5A99_ztu2lgYawAQ=="><div class="yt-lockup-dismissable"><div class="yt-lockup-thumbnail contains-addto polymer-thumbnail-padding"><a aria-hidden="true" href="/watch?v=sAwFLtbc7_c" class=" yt-uix-sessionlink spf-link " data-sessionlink="itct=CJcBEJQ1GAQiEwjOnsu1ou_RAhVJdaoKHVzWA1Eojh4yCmctaGlnaC10cnZaD0ZFd2hhdF90b193YXRjaA" ><div class="yt-thumb video-thumb"><span class="yt-thumb-simple">
<img width="196" data-ytimg="1" height="110" alt="" src="https://i.ytimg.com/vi/sAwFLtbc7_c/hqdefault.jpg?custom=true&w=196&h=110&stc=true&jpg444=true&jpgq=90&sp=68&sigh=qC9IiuL6075XOXO7ipK_PmTwmx8" onload=";__ytRIL(this)" >
<span class="video-time" aria-hidden="true">5:24</span></span></div></a> <span class="thumb-menu dark-overflow-action-menu video-actions">
<button type="button" aria-expanded="false" class="yt-uix-button-reverse flip addto-watch-queue-menu spf-nolink hide-until-delayloaded yt-uix-button yt-uix-button-dark-overflow-action-menu yt-uix-button-size-default yt-uix-button-has-icon no-icon-markup yt-uix-button-empty" onclick=";return false;" aria-haspopup="true" ><span class="yt-uix-button-arrow yt-sprite"></span><ul class="watch-queue-thumb-menu yt-uix-button-menu yt-uix-button-menu-dark-overflow-action-menu hid"><li role="menuitem" class="overflow-menu-choice addto-watch-queue-menu-choice addto-watch-queue-play-next yt-uix-button-menu-item" data-action="play-next" onclick=";return false;" data-video-ids="sAwFLtbc7_c"><span class="addto-watch-queue-menu-text">Play next</span></li><li role="menuitem" class="overflow-menu-choice addto-watch-queue-menu-choice addto-watch-queue-play-now yt-uix-button-menu-item" data-action="play-now" onclick=";return false;" data-video-ids="sAwFLtbc7_c"><span class="addto-watch-queue-menu-text">Play now</span></li></ul></button>
</span>
我试图用 grep 提取的文本expr
是 YouTube 用户名:TheEllenShow