删除指定字符之间的所有内容,包括多行 [英] Remove everything between specified characters including multi lines
本文介绍了删除指定字符之间的所有内容,包括多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文件,其内容如下:
I have a file with contents like so:
## this must go ##
## also
this
must go
##
hello world
##and this one
too##
我想删除 ## 之间的所有内容,包括多行内容,所以我只剩下hello world
I want to remove all between ##, including multi lines, so I am left just with hello world
这将仅删除一行上的部分:
This removes only part that is on one line:
sed -i.bak 's/##.*##//g' myfile
如何也删除多行内容?
MAC上的PS Im
P.S Im on MAC
推荐答案
您可以使用perl
来实现所需的目标:
You may use perl
to achieve what you want:
perl -0pe 's/##.*?##\R*//gs' file > newfile
请参见在线演示
0
参数使跨行查找匹配成为可能.
The 0
argument makes it possible to find matches across lines.
模式匹配
-
##
-两个#
符号 -
.*?
-尽可能少的0 +字符(由于s
修饰符而导致换行符) -
##
-两个#
符号 -
\R*
-任何0+个换行符序列.
##
- two#
symbols.*?
- any 0+ chars (even line break chars due to thes
modifier) as few as possible##
- two#
symbols\R*
- any 0+ line break sequences.
这篇关于删除指定字符之间的所有内容,包括多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文