格式化已解析的输出perl

问题描述 投票:0回答:1

我有这个文件-我需要的只是文件的最后五行。我知道我不应该在没有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)
perl format
1个回答
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)   
© www.soinside.com 2019 - 2024. All rights reserved.