sed multiline replace

99封情书 提交于 2019-11-29 09:17:12
William Pursell

If you are trying to delete lines with the literal text 'TAG', try:

sed '/\/\/<<<TAG/,/\/\/TAG>>>/d'

From your comments, it appears that TAG may not be literal, in which case:

sed '/^\/\/<</,/^\/\/.*>>/d'

This can be simplified by using a different delimiter:

sed '@^//<<<@,@^//.*>>>@d'

Rather than using the sed solution I gave, you might like either of these in perl and awk:

perl -ne 'print if !( m@//<<<TAG@ .. m@//TAG>>>@ )'
awk '/\/\/<<<TAG/,/\/\/TAG>>>/ {next} 1'

Given that I think you really do not want TAG to be a constant, the cleanest solution I know of is the perl variant:

perl -ne 'print if !( m@^//<<<(.*)@ .. m@^//$1>>>$@ )'

In addition the search delimiters in sed can be changed by escaping the first delimiter:

sed '\|^//<<<|,\|^//.*>>>|d' file

Awk version the matches the end with the same tag name:

awk -F'//<<<|//|>>>' '$2{p=$2; while(getline && p!=$2); next}1' file
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!