使用 bash/Perl 中的正则表达式从 html 表中提取值

发布于 2024-10-02 10:56:48 字数 2237 浏览 4 评论 0原文

我想用 munin 监控我的 oki 打印机,所以我尝试改编这个插件 到我的打印机。

我的打印机 http 服务器中的页表是:

<table width="560" border="0" cellspacing="2" cellpadding="3">
    <tr class="sub_item_color">
        <td  class="normal" width="200" align="right" valign="bottom" rowspan="2">Media Size</td>
        <td  class="normal" width="90" align="left">Color</td>
        <td  class="normal" width="90" align="left">Color</td>
        <td  class="normal" width="90" align="left">Mono</td>
        <td  class="normal" width="90" align="left">Mono</td>
    </tr>
    <tr class="sub_item_color">
        <td  class="normal" width="90" align="left">A3/Tabloid</td>
        <td  class="normal" width="90" align="left">A4/Letter</td><td  class="normal" width="90" align="left">A3/Tabloid</td>
        <td  class="normal" width="90" align="left">A4/Letter</td>
    </tr>
    <tr class="sub_item_color">
        <td  class="normal" width="200" align="left">Total Impressions</td>
        <td  class="normal" width="90" align="right">21906</td>
        <td  class="normal" width="90" align="right">33491</td>
        <td  class="normal" width="90" align="right">2084</td>
        <td  class="normal" width="90" align="right">4460</td>
    </tr>
    <tr class="sub_item_color">
        <td  class="normal" width="200" align="left">Total A4/Letter Impressions</td>
        <td  class="normal" colspan="2" align="center"><b>Color:77303</B></td>
        <td  class="normal" colspan="2" align="center"><b>Mono:8628</B></td>
    </tr>
</table>

munin 脚本正在执行以下操作:

infopage=`wget -q -O - http://root:$password@$destination/printer/printerinfo_top.htm | perl -p -e 's/\n/ /m'`
echo tray1.value    `echo $infopage | perl -p -e 's/^.+Tray\ 1\ Page\ Count\:\ \<\/TD\>\<TD\ WIDTH\=\"94\"\>([0-9]+)\<.+$/$1/'`

我如何获得总印数?

I want to monitor my oki printer with munin, so I am trying to adapt this plugin to my printer.

The table of pages in my printer http server is:

<table width="560" border="0" cellspacing="2" cellpadding="3">
    <tr class="sub_item_color">
        <td  class="normal" width="200" align="right" valign="bottom" rowspan="2">Media Size</td>
        <td  class="normal" width="90" align="left">Color</td>
        <td  class="normal" width="90" align="left">Color</td>
        <td  class="normal" width="90" align="left">Mono</td>
        <td  class="normal" width="90" align="left">Mono</td>
    </tr>
    <tr class="sub_item_color">
        <td  class="normal" width="90" align="left">A3/Tabloid</td>
        <td  class="normal" width="90" align="left">A4/Letter</td><td  class="normal" width="90" align="left">A3/Tabloid</td>
        <td  class="normal" width="90" align="left">A4/Letter</td>
    </tr>
    <tr class="sub_item_color">
        <td  class="normal" width="200" align="left">Total Impressions</td>
        <td  class="normal" width="90" align="right">21906</td>
        <td  class="normal" width="90" align="right">33491</td>
        <td  class="normal" width="90" align="right">2084</td>
        <td  class="normal" width="90" align="right">4460</td>
    </tr>
    <tr class="sub_item_color">
        <td  class="normal" width="200" align="left">Total A4/Letter Impressions</td>
        <td  class="normal" colspan="2" align="center"><b>Color:77303</B></td>
        <td  class="normal" colspan="2" align="center"><b>Mono:8628</B></td>
    </tr>
</table>

That munin script is doing this:

infopage=`wget -q -O - http://root:$password@$destination/printer/printerinfo_top.htm | perl -p -e 's/\n/ /m'`
echo tray1.value    `echo $infopage | perl -p -e 's/^.+Tray\ 1\ Page\ Count\:\ \<\/TD\>\<TD\ WIDTH\=\"94\"\>([0-9]+)\<.+$/$1/'`

How I could get the total impressions?

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

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

发布评论

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

评论(1

沉睡月亮 2024-10-09 10:56:48

解决方案作为 Unix 过滤器实现,就像问题中一样,由于 XPath,它的可读性和声明性更强。

#!/usr/bin/env perl
use 5.010;
use strictures;
use HTML::TreeBuilder::XPath qw();
use List::Util qw(sum);
my $tree = HTML::TreeBuilder::XPath->new;
$tree->parse_content(<>);
say sum map { s[.*:][]; $_ } $tree->findnodes_as_strings('//table/tr/td[@colspan=2]/b');

wget -q -O - http://… | perl sum-total-impressions.pl

Solution implemented as a Unix filter like in the question, only much more readable and declarative thanks to XPath.

#!/usr/bin/env perl
use 5.010;
use strictures;
use HTML::TreeBuilder::XPath qw();
use List::Util qw(sum);
my $tree = HTML::TreeBuilder::XPath->new;
$tree->parse_content(<>);
say sum map { s[.*:][]; $_ } $tree->findnodes_as_strings('//table/tr/td[@colspan=2]/b');

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