Link to home
Start Free TrialLog in
Avatar of Caiapfas
Caiapfas

asked on

find and replace with perl...

ok, Im trying to make this script find  _Category_ and _NUMBER_ and replace them with the right values, example :
lets say the Category is Texas, so it finds all _Category_ in the template and replaces it will Texas
Example 2 :
it finds all _NUMBER_  and replaces it with how many links it added in the catrgory/page.
and then continues with the present save parameters..

and these options are only vailid/work under option E...



----- add.pl --------


#!/usr/bin/perl
# $dir is the directory with the html pages (e.g. c:\\concert)
# $wdir is a path the path to be inserted in the URLs (e.g. /whatever/concerts)
# $indextpl is the index template file (e.g. C:\\test\\index.tpl)
# $indexhtm is the final index file (e.g. c:\\test\\index.htm)
# $dumphtm is the output file containing links to pages not inserted in the index (e.g. c:\\test\\dump.htm)
# $cattpl the template for each category with the string <!-- START --> inside
# $catdir path where category files should be created


# Run with command
# <scriptname> M
# for old beheviour and
# <scriptname> E
# for new "each category" behaviour


$dir="C:\\web\\venues";
$wdir="http://www.meme.com/venues";
$indextpl = "C:\\aa\\linkadd.automation\\venuemainindex.temp.htm";
$indexhtm = "C:\\aa\\linkadd.automation\\llaindex.htm";
$dumphtm = "C:\\aa\\linkadd.automation\\dump.htm";


# In the latter case, two new variable define file and paths:
$cattpl = "C:\\aa\\linkadd.automation\\venuecategory.temp.htm";
$catdir = "C:\\aa\\linkadd.automation\\categories\\";

if($#ARGV != 0 || $ARGV[0] !~ /^[me]$/i) {
  print "Usage: $0 {m|e}\n";
  exit;
}

# read venue files
opendir(DH, $dir) or die "Can't open $dir for reading: $!";
while(defined($file=readdir(DH))) {
  next unless $file =~ /\.htm$/i;
  open(FILE, "<$dir/$file") or die "Can't open $file for reading: $!";
  $ven{lc($1)}{$2} = "$wdir/$file" if <FILE> =~ /^\<\!-- (.*), (.*) --\>$/;
  close(FILE);
}

# process template
my $tot=0;
if(lc($ARGV[0]) eq "m") {
  open(IDXIN, "<$indextpl") or die "Can't open $indextpl for reading: $!";
  open(IDXOUT, ">$indexhtm") or die "Can't open $indexhtm for writing: $!";
  while(<IDXIN>) {
    if(/^\<\!-- (.*) --\>$/) {
      my @vkeys = sort keys %{$ven{lc($1)}};
      print IDXOUT map {"<A href=\"$ven{lc($1)}{$_}\">$_ event tickets</A><BR>\n"} @vkeys;
      $tot += @vkeys;
      print "\L$1: " . @vkeys . "\n";
      delete($ven{lc($1)});
    } else {
      print IDXOUT;
    }
  }
  close(IDXIN);
  close(IDXOUT);
  print "Total: $tot\n";

  # dump leftovers
  print "\nOrphans:\n";
  $tot=0;
  open(ORPOUT, ">$dumphtm") or die "Can't open $indexhtm for writing: $!";
  foreach $k (sort keys %ven) {
    my @vkeys = sort keys %{$ven{$k}};
    print ORPOUT map {"<A href=\"$ven{$k}{$_}\">$_</A><BR>\n"} @vkeys;
    $tot += @vkeys;
    print "$k: " . @vkeys . "\n";
  }
  close(ORPOUT);
  print "Total: $tot\n";

} else {
     
  open(CATIN, "<$cattpl") or die "Can't open $cattpl for reading: $!";
  {
    local $/;
    $catfile = <CATIN>;
  }
  close(CATIN);
  $catfile =~ /^(.*)\<\!-- START --\>(.*)$/si;
  foreach $k (sort keys %ven) {
    my @vkeys = sort keys %{$ven{$k}};
    open(CATOUT, ">$catdir/$k.htm") or die "Can't open $catdir/$k.htm for writing: $!";
    print CATOUT $1;
    print CATOUT map {"<A href=\"$ven{$k}{$_}\">$_ event tickets</A><BR>\n"} @vkeys;
    print CATOUT $2;
    close(CATOUT);
    $tot += @vkeys;
    print "$k: " . @vkeys . "\n";
  }
  print "Total: $tot\n";
}
Avatar of Kavar
Kavar

if(/^\<\!-- (.*) --\>$/) {
      s/_Category_/NewCategory/g;
      s/_Number_/NewNumber/g;
      my @vkeys = sort keys %{$ven{lc($1)}};
...etc
ASKER CERTIFIED SOLUTION
Avatar of lbertacco
lbertacco

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial