1

I am stumped on why this sed command would not be working:

sed 's/<script id="live-reload"[\s\S]*?<\/script>/test/g' www_public/index.html > www_public/index.html.temp

It should replace the live-reload script tag of the following HTML with 'test':

<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
    <head>

        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <title>AssembleJS</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width">

        <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico">
        <link rel="stylesheet" href="assets/css/main.css">

    </head>

    <body>

        <script id="require-lib" data-main="src/config" src="src/libs/require.js"></script>

        <script id="live-reload">document.write('<script src="http://' + (location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=1"></' + 'script>')</script>

        <script id="test"> bla bla bla </script>

    </body>

</html>

Here's the regex working:

http://regexr.com?339ga

0

1 Answer 1

3

You need to use -r for ? as it's extended regex:

$ sed -r 's/<script id="live-reload".*?<\/script>/test/g' file

or escape it:

$ sed 's/<script id="live-reload".*\?<\/script>/test/g' file

Output:

<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
    <head>

        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <title>AssembleJS</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width">

        <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico">
        <link rel="stylesheet" href="assets/css/main.css">

    </head>

    <body>

        <script id="require-lib" data-main="src/config" src="src/libs/require.js"></script>

        test

        <script id="test"> bla bla bla </script>

    </body>

</html>

Notes:

  • Also changed [\s\S]*? to .*? as they're complimentary groups.

  • The g flag is only necessary for multiple matches on a single line.

  • If you're running Mac the -r flag is -E so check with sed --help for the extended regex option.

  • Obligatory don't parse [x]html with regex warning, using a parser.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.