sed多行替换

sed多行替换,sed,multiline,Sed,Multiline,这是我的示例文本文件: asdas //<<<TAG this should be removed //TAG>>> this should be there //<<<TAG T > asd asd //TAG>>> 阿斯达斯 // 这个应该在那里 //> 我希望其o/p为: asdas this should be there 阿斯达斯 这个应该在那里 基本上,我试图在“/”(也包括这些行)之间找到行并删除它们 我试着

这是我的示例文本文件:

asdas //<<<TAG this should be removed //TAG>>> this should be there //<<<TAG T > asd asd //TAG>>> 阿斯达斯 // 这个应该在那里 //> 我希望其o/p为:

asdas this should be there 阿斯达斯 这个应该在那里 基本上,我试图在“/”(也包括这些行)之间找到行并删除它们

我试着用sed

sed-n '1h;1.H${;g;s//>///g;p;}' 但也有人认为它并没有产生正确的产出。包含“>”符号的第二个标记在正则表达式中失败。不知道我哪里出错了


知道怎么做吗

如果您试图删除带有文字“TAG”的行,请尝试:

sed '/\/\/<<<TAG/,/\/\/TAG>>>/d' sed'/\/\//d' 从您的评论来看,标记可能不是文字,在这种情况下:

sed '/^\/\/<</,/^\/\/.*>>/d' sed'//^\//\//d' 这可以通过使用不同的分隔符来简化:

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

您可能喜欢perl和awk中的任何一种,而不是使用我给出的sed解决方案:

perl -ne 'print if !( m@//<<<TAG@ .. m@//TAG>>>@ )' awk '/\/\/<<<TAG/,/\/\/TAG>>>/ {next} 1' perl-ne'打印如果!(m//)" awk'/\/\/{next}1' 鉴于我认为您确实不希望标记是常量,我所知道的最干净的解决方案是perl变体:

perl -ne 'print if !( m@^//<<<(.*)@ .. m@^//$1>>>$@ )' perl-ne'打印如果!(百万元)"
此外,可以通过转义第一个分隔符来更改
sed
中的搜索分隔符:

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

Awk版本与具有相同标记名的端点匹配:

awk -F'//<<<|//|>>>' '$2{p=$2; while(getline && p!=$2); next}1' file
awk-F'/'$2{p=$2;while(getline&&p!=$2);next}1'文件

在您的输出中,您确定要在asdas和“this should be there”之间留一个空行吗?谷歌搜索“sed one liners”通常非常有用-应该是首批结果之一,并且包含这一条。