我有一个简单的bash脚本。它从文件test.html
中读取一堆 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
我想出的解决方案如下。请记住,这是一个"勉强有效"的解决方案,我只是使用它,以便我可以完成我的项目并继续前进。正如Chepener所说,expr不是这项工作的好工具,我只是因为我被要求使用它。
grep yt-lockup-content test.html | while read -r line; do
users=`expr "$line" : ".*[a-zA-Z0-9]" >(.*)</a>[</div>? ?]*"`; echo ${users}
done