使用awk或sed删除两个图案(包括图案)之间的线条

使用awk或sed删除两个图案(包括图案)之间的线条,awk,sed,Awk,Sed,我的输出文件如下: judi#cat file ---ABC--- word1 word2 word3 word4 word5 word6 ---end_ABC--- ---DEF--- line1 line2 line3 line4 ---end_DEF--- judi# 我需要删除模式ABC和end\u ABC之间的行(包括模式,然后替换为新内容;新内容位于文件中) 文件的内容各不相同,因此我只需要使用模式 judi#file1 ---ABC--- wordA1 wordA2 wordA

我的输出文件如下:

judi#cat file
---ABC---
word1
word2
word3
word4
word5
word6
---end_ABC---

---DEF---
line1
line2
line3
line4
---end_DEF---
judi#
我需要删除模式
ABC
end\u ABC
之间的行(包括模式,然后替换为新内容;新内容位于文件中)

文件的内容各不相同,因此我只需要使用模式

judi#file1
---ABC---
wordA1
wordA2
wordA3
---end_ABC---
judi#
所期望的结果必须是

judi#
---ABC---
wordA1
wordA2
wordA3
---end_ABC---

---DEF---
line1
line2
line3
line4
---end_DEF---
judi#
我试过这个命令:

sed '/ABC/,/end_ABC/{/ABC/!{/end_ABC/!d}}' file > file 2
但我得到了这个错误:

sed: command garbled: /ABC/,/end_ABC/{/ABC/!{/end_ABC/!d}}
输出

judi#cat file
judi#file1
---ABC---
wordA1
wordA2
wordA3
---end_ABC---
judi#

---DEF---
line1
line2
line3
line4
---end_DEF---
judi#
a###here
在匹配
end#u ABC
后追加
###here


r file1
在查找模式
##后插入file1中的文本

永远不要使用范围表达式,因为它们使琐碎的任务变得非常简单,但即使稍微复杂的任务也需要完全重写或复制条件。只需使用一个标志:

awk '
NR==FNR { rep = rep $0 OFS; next }
/---ABC---/ { printf "%s", rep; inBlock=1 }
!inBlock
/---end_ABC---/ { inBlock=0 }
' file1 file

你的命令在GNU sed下对我有效。如果您使用的是BSD(OSX)SED,请考虑在关闭括号之前添加一些分号:<代码> } /代码> ->代码>;}
.yes命令看起来不错(GNU-sed),只有文件+2,该命令将删除范围内的所有行,不包括第一行和最后一行。在发布之前,请学习搜索S.O。您的标题“删除两个模式之间的行”(模式上带有“s”)返回76项。祝你好运。我想我用的是其他的sed而不是你提到的枪sed。。获取以下错误“sed:command-charbled:/end_ABC/a###here”如何检查正在使用的sed?sed--version+
awk '
NR==FNR { rep = rep $0 OFS; next }
/---ABC---/ { printf "%s", rep; inBlock=1 }
!inBlock
/---end_ABC---/ { inBlock=0 }
' file1 file