[英]Perl replace not working as expected
I want to replace the multiple js file block to a minified version using perl command. 我想使用perl命令将多个js文件块替换为缩小版本。
When I tested it on online website, it works but when I run in command line, it does not. 当我在在线网站上测试它时,它可以工作,但是当我在命令行中运行时,它却不能。
echo '<!doctype html>
<html>
<head>
<script src="lib/angular.js"></script>
<!-- build:js ../ute.min.js -->
<script src="app/app.js"></script>
<script src="app/services/uteEndpoint.js"></script>
<!-- endbuild:js -->' | perl -pe 's/<!-- build:js ([^ ]+) -->[^\!]+<!-- endbuild:js -->/<script src="$1"><\/script>/gm'
What am I doing wrong in command line? 我在命令行做错了什么?
I have tested here, http://www.regexe.com/ , and my expected output is 我已经在这里http://www.regexe.com/进行了测试,我的预期输出是
<!doctype html>
<html>
<head>
<script src="lib/angular.js"></script>
<script src="../ute.min.js"></script>
By default Perl processes the input line by line. 默认情况下,Perl逐行处理输入。 Since you're working with multiple lines, you need to enable paragraph mode.
由于您要处理多行,因此需要启用段落模式。
perl -00pe 's~<!-- build:js ([^ ]+) -->[^\!]+<!-- endbuild:js -->~<script src="$1"></script>~gm'
OR 要么
perl -0777pe 's~<!-- build:js ([^ ]+) -->[^\!]+<!-- endbuild:js -->~<script src="$1"></script>~gm'
Example: 例:
$ echo '<!doctype html>
> <html>
> <head>
> <script src="lib/angular.js"></script>
> <!-- build:js ../ute.min.js -->
> <script src="app/app.js"></script>
> <script src="app/services/uteEndpoint.js"></script>
> <!-- endbuild:js -->' | perl -0777pe 's/<!-- build:js ([^ ]+) -->[^\!]+<!-- endbuild:js -->/<script src="$1"><\/script>/gm'
<!doctype html>
<html>
<head>
<script src="lib/angular.js"></script>
<script src="../ute.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.