我有很多文件包含类似的字符串,例如多行字符串:
<script> var i = 100
var j = 200
var x = 1000 </script>
它看起来像这样:
<script> var i = 100
var j = 200
var x = 1000 </script>
或
<script> var i = 100
var j = 200
var x = 1000 </script>
我希望用
替换它<script> var i = 100
var j = 200
var x = xxxx </script>
请注意,该行也可以是无间距的,有时也可以是标签
我遇到问题的情况是多线,如果它是简单的一行,那么它很容易,
答案 0 :(得分:0)
perl中的多行替换很容易:
perl -0 -pe 's/<script>\s*var\s+i\s+=\s+100\s+var\s+j\s+=\s+200\s+var\s+x\s+=\s+1000\s+<\/script>/<script> var i =100\n var j =100\n var x = xxxx <\/script>/g' input-file
或(略微可读):
perl -0 -pe 's/<script>\s*
var\s+ i\s+ =\s+ 100\s+
var\s+ j\s+ =\s+ 200\s+
var\s+ x\s+ =\s+1000\s+
<\/script>/<script> var i =100\n var j =100\n var x = xxxx <\/script>/gx' input-file