Hello,
thanks for getting back to my problem.
Ok, let's see - I have this pretty ugly code I managed to write and it is working. I'm sure there are nicer ways to achieve my goal but I'm a complete beginner - sorry:
#!/usr/bin/perl
use LWP::Simple;
$elem1 = "http://www.test.de/subfolder/";
$elem3 = "http://www.test.de/subfolder/de/";
# --------- Logfile ----------------------------
my $delimiter = "\t";
my $logfile = "errorlog.txt";
my $datum = localtime();
my $logmsg = "$datum $ENV{USER} Broken Links";
open LOGFILE, ">$logfile" or die $!;
print LOGFILE $logmsg, "\n";
# --------- Open File -----------------------
open(READ,"Linkfile.csv") or die $!;
while (my $line = <READ>){
if($line=~/\d+\t/){
my @liste = split($delimiter,$line);
push(@urls,$liste[2]);
}
}
close READ;
# ----------------------------------------------
foreach (@urls) {
chomp($_);
if (head($_)) {
print $_." is working.\n\n";
} else {
print $_." is broken.\n\n";
open LOGFILE, ">>$logfile" or die $!;
print LOGFILE "\n".$_." is broken.";
close LOGFILE;
$tmp_url = "$_.";
@array=split(/\?/,$_);
$myString = $array[0];
if ($myString =~ m#http:\/\/www.test.de\/subfolder\/de\/#) {
$myString =~ s#http:\/\/www.test.de\/subfolder\/de\/##;
$newUrl = $elem1.$myString;
} else {
$myString =~ s#http:\/\/www.test.de\/subfolder\/##;
$newUrl = $elem3.$myString;
print $newUrl." generated \n\n";
}
if (head($newUrl)) {
print $newUrl." is working.\n\n";
open LOGFILE, ">>$logfile" or die $!;
print LOGFILE "\n".$newUrl." is working.\n";
close LOGFILE;
} else {
print $newUrl." isn't working, too.\n\n ";
open LOGFILE, ">>$logfile" or die $!;
print LOGFILE "\n".$newUrl." isn't working, too.\n";
close LOGFILE;
}
}
}
Basicaly it does the following:
Open the File, gettinge the 3. tabulated text and testing it if the server responds with a 200 (all ok) or 404 (not found.)
If the Link is broken, it is testing it if there's only a problem with the subfolder and generating a new url. Testing the head of this link.
So I have a file now with all the broken links - what I want to do is to delete these broken links in my original file.
PROBLEM is that I don't want to delete just the link but the whole line in the original file.
I'm sorry if I can't describe it better - English is not my mother tongue ;)
Thanks in advance.
Regards, Robert |