remove some tags in multiple files

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
hello all,

I'm not sure if this would be the appropriate groupd, but it concerns html
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

Webmaster of: / /

Re: remove some tags in multiple files

Tomba wrote:

Quoted text here. Click to load it

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");

Site Timeline