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:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.