D&C GLug - Home Page

[ Date Index ] [ Thread Index ] [ <= Previous by date / thread ] [ Next by date / thread => ]

Re: [LUG] Mass editing text files?

 

On Thu, 26 Jul 2007 20:43:15 +0100
"Jonathan Roberts" <jonathan.roberts.uk@xxxxxxxxxxxxxx> wrote:

> I have a load of HTML files and I want to add the same text to the
> <head> section of all of them: is there any way I can add it
> automatically?! sed perhaps?

I'd use perl with something like: 
opendir(FH, "dir") or die ("message: $!);
@files=grep(!/^\.\.?*.html/, readdir(FH));
closedir(FH);

foreach $file (@files)
{
        open (FILE, "dir/$file") or die ("message2: $!);
        @contents=<FILE>;
        close (FILE);
# now parse each file, one line at a time via the @contents array

}

All of the above is off-the-cuff and may contain syntax errors.

You could, perhaps, check for the first line to contain "</head>" and
then insert your code before that line is written back out.

The problem with the sample code is that it slurps the entire file into
memory and needs to write the whole file out if there are any changes.

-- 

Neil Williams
=============
http://www.data-freedom.org/
http://www.nosoftwarepatents.com/
http://www.linux.codehelp.co.uk/

Attachment: pgpg6uaOqOC2S.pgp
Description: PGP signature

-- 
The Mailing List for the Devon & Cornwall LUG
http://mailman.dclug.org.uk/listinfo/list
FAQ: http://www.dcglug.org.uk/linux_adm/list-faq.html