用 sed 获取两个字符串之间的值?

发布于 2024-11-08 04:36:25 字数 836 浏览 0 评论 0原文

我在一行中有以下数据:

<a href="#page-metadata-start" class="assistive">Go to start of metadata</a>
<div id="page-metadata-end" class="assistive"></div>

        <fieldset class="hidden parameters">
                        <input type="hidden" title="browsePageTreeMode" value="view">
        </fieldset>

        <div class="wiki-content">
           <p>(openissues)81(/openissues)</p><p>(assignstoday)0(/assignstoday)</p><p>(assignsweek)2(/assignsweek)</p><p>(replyissues)6(/replyissues)</p><p>(wrapissues)26(/wrapissues)</p>
        </div>

例如,我想获取“openissues”的值,但我无法弄清楚如何正确检索它。我尝试过的事情之一是以下命令:

sed -n '/(assignstoday)/,/(\/assignstoday)/p' ~/test.txt

有帮助吗?

I have the following data on one line:

<a href="#page-metadata-start" class="assistive">Go to start of metadata</a>
<div id="page-metadata-end" class="assistive"></div>

        <fieldset class="hidden parameters">
                        <input type="hidden" title="browsePageTreeMode" value="view">
        </fieldset>

        <div class="wiki-content">
           <p>(openissues)81(/openissues)</p><p>(assignstoday)0(/assignstoday)</p><p>(assignsweek)2(/assignsweek)</p><p>(replyissues)6(/replyissues)</p><p>(wrapissues)26(/wrapissues)</p>
        </div>

I'd like to grab the value for "openissues" for example, but I can't figure out to properly retrieve this. One of the things I tried is the following command:

sed -n '/(assignstoday)/,/(\/assignstoday)/p' ~/test.txt

Any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

渡你暖光 2024-11-15 04:36:25
sed 's/.*(openissues)\(.*\)(\/openissues).*/\1/' test.txt

一个可能满足您编辑要求的快速技巧:

sed -n '/openissues/p' test.txt | sed  's/.*(openissues)\(.*\)(\/openissues).*/\1/'

但是在解析 HTML 时,正则表达式确实不是正确的方法。

sed 's/.*(openissues)\(.*\)(\/openissues).*/\1/' test.txt

a quick hack to possibly meet your edited requirement:

sed -n '/openissues/p' test.txt | sed  's/.*(openissues)\(.*\)(\/openissues).*/\1/'

but regexes are really not the way to go when parsing HTML.

予囚 2024-11-15 04:36:25

我会尝试

VALUE=openissues
sed  's@.*('"$VALUE"')\([^(]\+\).*@\1@'

用该内容替换除您正在搜索的内容之外的所有内容。

编辑:现在我看到尼尔的答案,几乎是一样的,接受他的。我留下我的答案来定制您想要提取的值。

I'd try

VALUE=openissues
sed  's@.*('"$VALUE"')\([^(]\+\).*@\1@'

that is, replace everything except the contents of what you are searching, with that content.

edit: Now I see Neil's answer, that's practically the same, accept his. I leave my answer for the customization of which value you want to extract.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文