views:

80

answers:

3
my $changelog = "/etc/webmin/Pserver_Panel/changelog.cgi";
my $Milestone;
open (PREFS, $changelog);
 while (<PREFS>)
 {
  if ($_ =~ m/^<h1>(.*)[ ]Milestone.*$/g) {
   $Milestone=$1;
   last;
  }
 }
close(PREFS);

Here is an example of the data its extracting from:

<h1>1.77 Milestone</h1>
    <h3> 6/26/2009  </h3><ul style="margin-top:0px">
     <li type=circle> Standard code house cleaning and added better compatbility for apache conversion.
    </ul>
    <h3> 6/21/2009  </h3><ul style="margin-top:0px">
     <li type=square> Fixed Autofix so that it extracts to the right directory.
    </ul>
    <h3> 6/11/2009  </h3><ul style="margin-top:0px">
     <li type=circle> Updated FTP link on index page to go to net2ftp, an online ftp file manager.
    </ul>
<h1>1.76 Milestone</h1>
    <h3> 4/14/2009  </h3><ul style="margin-top:0px">
     <li type=square> Corrected a broken hyperlink to regular expressions in "View Chat Log"
     <li type=circle> Changed the default number of lines back from 25 to 10 on both Chat and Pserver Logs.
     <li type=circle> Noted in "View Pserver Log" search is case-sensitive and regular expression supported.
    </ul>
    <h3> 4/13/2009  </h3><ul style="margin-top:0px">
     <li type=disc>  Added AutoFix to the panel which will automatically fix prop errors.
     <li type=circle> Updated error display to allow more detailed errors.
    </ul>
    <h3> 4/12/2009  </h3><ul style="margin-top:0px">
     <li type=circle> Fixed start/stop/restart to be more reliable.
    </ul>
A: 

I you want a one-liner, how about this one?

perl -nle 'if (/^<h1>(.*)[ ]Milestone.*$/g){ print $1; last }' /etc/webmin/Pserver_Panel/changelog.cgi
innaM
The 'g' modifier is useless in this case, as is the trailing '.*$' part of the regex.
Yanick
+3  A: 

Next, you are going to have to parse the items between milestones. Do yourself a favor, stop worrying about lines of code and use an HTML Parser, such as HTML::TokeParser:

#!/usr/bin/perl

use strict;
use warnings;

use HTML::TokeParser;

my $parser = HTML::TokeParser->new( \*DATA );

while ( my $token = $parser->get_token ) {
    if ( $token->[0] eq 'S' ) {
        if ( $token->[1] eq 'h1') {
            my ($milestone) = split ' ', $parser->get_text('/h1');
            print "Milestone is '$milestone'\n";
        }
    }
}

__DATA__
<h1>1.77 Milestone</h1>
...

C:\Temp> vbn
Milestone is '1.77'
Milestone is '1.76'
Sinan Ünür
Note that this will continue to work if the lines are merged, or split up among multiple lines.
Brad Gilbert
A: 

Another one-liner to grab it from the command line:

perl -ne'print $1 and exit if /<h1>(.*?)\s+Milestone/' /etc/webmin/Pserver_Panel/changelog.cgi
Yanick