我正在尝试用空行替换两个模式之间的代码块 尝试使用以下命令
sed '/PATTERN-1/,/PATTERN-2/d' input.pl
但它只删除了模式之间的界限
PATTERN-1:“= head” PATTERN-2:“= cut”
input.pl包含以下文字
=head
hello
hello world
world
morning
gud
=cut
必需的输出:
=head
=cut
有人可以帮我吗?
答案 0 :(得分:1)
$ awk '/=cut/{f=0} {print (f ? "" : $0)} /=head/{f=1}' file
=head
=cut
答案 1 :(得分:1)
要修改给定的sed
命令,请尝试
$ sed '/=head/,/=cut/{//! s/.*//}' ip.txt
=head
=cut
//!
匹配起始/结束范围以外的其他内容,可能取决于sed
实现是否动态匹配两个范围或静态匹配其中一个。适用于GNU sed
s/.*//
清除这些行答案 2 :(得分:0)
awk '/=cut/{found=0}found{print "";next}/=head/{found=1}1' infile
# OR
# ^ to take care of line starts with regexp
awk '/^=cut/{found=0}found{print "";next}/^=head/{found=1}1' infile
说明:
awk '/=cut/{ # if line contains regexp
found=0 # set variable found = 0
}
found{ # if variable found is nonzero value
print ""; # print ""
next # go to next line
}
/=head/{ # if line contains regexp
found=1 # set variable found = 1
}1 # 1 at the end does default operation
# print current line/row/record
' infile
测试结果:
$ cat infile
=head
hello
hello world
world
morning
gud
=cut
$ awk '/=cut/{found=0}found{print "";next}/=head/{found=1}1' infile
=head
=cut
答案 3 :(得分:0)
这可能适合你(GNU sed):
sed '/=head/,/=cut/{//!z}' file
删除=head
和=cut
之间的行。