nafees has asked for the wisdom of the Perl Monks concerning the following question:

hello,
i'm trying to insert meta tags into html files. is this possible using a command line eg:

perl -pi -e 's/(<head>)/(<head><meta http-equiv=\"Content-Type\" conte +nt=\"text\/html; charset=ISO-8859-1\"\/>)/gi' *.htm*

which basically looks for <head> tags and adds the <meta...> to it. it gives me a "< expected" error.

can anyone plz help?

much appreciated.

Formatting and code tags added by davido.

Replies are listed 'Best First'.
Re: inserting into html files
by brian_d_foy (Abbot) on Feb 03, 2005 at 01:09 UTC

    This works fine for me in bash, so if you think it's a shell problem, tell us which shell you use.

    Also, I think you want a different replacement, since you don't want the parens to show up in the HTML document. Here's the same thing but cleansed a bit (including an alternate delimiter to get around escaping /)

    perl -pi -e 's|<head>|<head>\n<meta http-equiv="Content-Type" content= +"text/html; charset=ISO-8859-1"/>|i' test.html
    --
    brian d foy <bdfoy@cpan.org>
Re: inserting into html files
by Cody Pendant (Prior) on Feb 03, 2005 at 01:12 UTC
    I think you need to use less escaping!

    There are all kinds of things in that regex that don't need to be there.

    You don't need to escape the slashes if you don't use slash as the delimiter:s|foo|bar|, s#foo#bar#, s{foo}{bar} for instance.

    You also don't need brackets around either side, or to escape the double quotes inside the single-quotes, so instead of

    s/(<head>)/(<head><meta http-equiv=\"Content-Type\" content=\"text\/html; charset=ISO-8859-1\"\/>)/gi
    you could just use:
    s|<head>|<head><meta http-equiv="Content-Type" content="text\/html; charset=ISO-8859-1" />|gi

    The above works for me.



    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
    =~y~b-v~a-z~s; print
Re: inserting into html files
by friedo (Prior) on Feb 03, 2005 at 00:31 UTC
    Your code works fine for me. If I had to guess, I would say it's a problem with your shell interpreting quotes. You may need to add additional escaping.
      thanks for ur reply. this is dumb, but how do i add additional escaping?
Re: inserting into html files
by Anonymous Monk on Feb 03, 2005 at 11:08 UTC
    I also suggest using ISO-8859-15 instead of -1, which is deprecated because of lack of the € character.