我有一个简单的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&#39;s Comments on the Travel Ban" aria-describedby="description-id-183985" dir="ltr">Ellen&#39;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>&nbsp;<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&amp;w=196&amp;h=110&amp;stc=true&amp;jpg444=true&amp;jpgq=90&amp;sp=68&amp;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

最佳答案

我想出的解决办法如下。请记住,这是一个“只是勉强工作”的解决方案,我只是在使用它,以便我可以完成我的项目,并继续前进。正如切佩纳所说,expr不是一个很好的工具,我使用它只是因为我被要求。

  grep yt-lockup-content test.html | while read -r line; do
      users=`expr "$line" : ".*[a-zA-Z0-9]\" >\(.*\)</a>[</div>?&nbsp;?]*"`; echo ${users}
  done

关于regex - expr正则表达式中的变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42125716/

10-13 07:23