remove some tags in multiple files

T

Tomba

hello all,

I'm not sure if this would be the appropriate groupd, but it concerns html
files.
In fact I have a folder filled with html files, and I would like to remove
all from <head> to </head> in each file of that folder.

Does anyone know what would be the most convenient way to do this?
thanks in advance,

Steven De Groote
 
L

Leif K-Brooks

Tomba said:
In fact I have a folder filled with html files, and I would like to remove
all from <head> to </head> in each file of that folder.

Not sure why you'd want to do that, but try this Perl script:

use strict;
use warnings;

my $foldername = shift @ARGV or die("can't get folder name\n");
opendir(FOLDER, $foldername) or die("can't open $foldername: $!\n");
chdir($foldername) or die("can't change directory to $foldername: $!");
while (my $filename = readdir(FOLDER)) {
next unless ($filename =~ /\.html?$/i);
print "removing head of $filename\n";
open(FILE, $filename) or warn("can't open $filename for reading: $!\n");
my $file;
while (my $filepart = <FILE>) {
$file .= $filepart;
}
close FILE or warn("can't close $filename: $!\n");
$file =~ s#<head((?: [^>]*)?)>.*</head>#<head$1></head>#si;
open(FILE, ">$filename") or warn("can't open $filename for writing: $!\n");
print FILE $file;
close FILE or warn("can't close $filename: $!\n");
}
closedir FOLDER or die("can't close $foldername: $!\n");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top