格式化解析输出perl

时间:2014-11-27 06:49:04

标签: perl format

是的我有这个文件 - 所有我需要的是文件的最后五行。 我知道我不应该在没有html模块的情况下解析html。但这并非如此 程序严格 - 我的意思是我真正需要的是最后五行左右。除此之外,我无法下载 任何模块。我有权访问代理服务器,它允许我从命令行卷曲文件 所以也许有一种方法可以使用cpan fromteh或通过代理 - 但这是一个无关紧要的问题。 手头的问题是,当我解析出大部分文件行时,我不会得到 " MY-DEPT中的名字受到限制"  而且我想要它。它被跳过了。

new_guy@casper0170foo:~/hey/hit_BANK_restricted.$ cat restricted.html.bak
To:DL-BANK@big_business.com
From:dl-dept?g-gsd-stm@big_business.com
Subject: Restricted List for 25-Nov-2014
Content-Type: text/html;
Content-Transfer-Encoding: quoted-print HTMLFILEable>
<HTML>
  <HEAD>
     <STYLE type="text/css">
     body    { font-family: verdana; font-size: 10pt }
     td      { font-size: 8pt; vertical-align: top }
     td.cat  { color: 6699FF ; background: 666699; text-align: right; vertical-align: bottom; height: 30 }
     td.ind  { width: 20pt }
     td.link { }
     td.desc { color: a0a0a0 }
     a:visited { color: 800080; text-decoration: none }
    </STYLE>
 <TITLE>TRADES</TITLE>
 </HEAD><BODY><TABLE width="80%" border="0" cellpadding="0" cellspacing="0">
      <tr>
         <td colspan="3" align="center">Names IN MY-DEPT that are restricted</td>
      </tr>
      <tr>
        <td><b>Restriction Code</b></td>
        <td><b>Company</b></td>
        <td><b>Ticker</b></td>
     </tr><tr><td>RL5</td><td>First Trust Global Risk Managed Inc</td><td>ETP</td></tr><font color="red"><tr><td>RLMT</td><td>GT Advanced Technologies Inc</td><td nowrap>GTATQ (position only, not in MY-DEPT)</td></tr></font></TABLE></BODY</HTML>new_guy@casper0170foo:~/hey/hit_BANK_restricted.$
new_guy@casper0170foo:~/hey/hit_BANK_restricted.$
new_guy@casper0170foo:~/hey/hit_BANK_restricted.$
new_guy@casper0170foo:~/hey/hit_BANK_restricted.$
new_guy@casper0170foo:~/hey/hit_BANK_restricted.$
new_guy@casper0170foo:~/hey/hit_BANK_restricted.$
new_guy@casper0170foo:~/hey/hit_BANK_restricted.$
new_guy@casper0170foo:~/hey/hit_BANK_restricted.$ cat parse_restrict2
#!/usr/bin/perl
use strict;
use warnings ;
my @restrict_codes = qw(RL3 RL5 RL5H RL6 REGM RAF RLMT RTCA RTCAH RTCB RTCBH RTCI RTCIH RLSI RLHK RLJP RPROP RLCB RLCS RLBZ RLBZH RLSUS);
my $rest_dir = "/home/new_guy/hey/hit_BANK_restricted./";
my $restrict_file = "restricted.html.bak" ;
open my $fh_rest_codes, '<', "$rest_dir$restrict_file" or die "cannot load $! " ;
while (<$fh_rest_codes>) {
    next unless $_ =~ m/Names/;
    my @lines = <$fh_rest_codes> ;
    }
foreach(@lines) {
    s/td/ /g ;
    s/<[^>]*>/ /g ;
    foreach $restrict(@restrict_codes) {
        s/$restrict/\n$restrict/g;
        }
    print $_ ;
    sleep 1  ;
    }

print "\n" ;

这些是我得到的结果: 他们很好,但我想格式化他们,我不知道如何。

new_gue@casper0170foo:~/hey/hit_BANK_restricted.$ cat parse_restrict^C
new_guy@casper0170foo:~/hey/hit_BANK_restricted.$ ./parse_restrict2


          Restriction Code
          Company
          Ticker

RL5  First Trust Global Risk Managed Inc  ETP
RLMT  GT Advanced Technologies Inc  GTATQ (position only, not in MY-DEPT)
new_guy@casper0170foo:~/hey/hit_BANK_restricted.$
new_guy@casper0170foo:~/hey/hit_BANK_restricted.$
new_guy@casper0170foo:~/hey/hit_BANK_restricted.$
new_guy@casper0170foo:~/hey/hit_BANK_restricted.$
new_guy@casper0170foo:~/hey/hit_BANK_restricted.$

是否有可能以这种格式获取线条。

Names IN MY-DEPT that are restricted

Restriction Code Company                              Ticker
RL5              First Trust Global Risk Managed Inc  ETP
RLMT             GT Advanced Technologies Inc         GTATQ (position only, not in MY-DEPT)

1 个答案:

答案 0 :(得分:1)

好问题,如果您愿意,可以尝试这种解决方法:

my @lines;
while (<$fh_rest_codes>) {
    next unless $_ =~ m/Names/;
    push(@lines, $_);
    push (@lines, <$fh_rest_codes>);
}
my $str=join ('',@lines);    
$str=~m|<td.*?>(.*?)</td>|;    
print "$1\n\n";
$str=~ m|<tr>(.*?)</tr>|msg;
my $fmt="%-24s%-40s%-40s\n";
printf ($fmt,  $1=~ m{<td><b>(.*?)</b></td>}msg );
while ($str=~ m|<tr>(.*?)</tr>|msg) {
    printf ($fmt,  $1=~ m{<td.*?>(.*?)</td>}msg );
}

输出:

Names IN MY-DEPT that are restricted

Restriction Code        Company                                 Ticker                                  
RL5                     First Trust Global Risk Managed Inc     ETP                                     
RLMT                    GT Advanced Technologies Inc            GTATQ (position only, not in MY-DEPT)