问题描述
aaa
Any--END--Pattern
bbb
ANY--BEGIN--PATTERN
ccc # do not print
ANY--BEGIN--PATTERN # print 1
ddd # print 2
Any--END--Pattern # print 3
eee
fff
ANY--BEGIN--PATTERN # print 4
ggg # print 5
Any--END--Pattern # print 6
hhh # print 7
Any--END--Pattern # print 8
iii # do not print
ANY--BEGIN--PATTERN
jjj
想要的输出
ANY--BEGIN--PATTERN # print 1
ddd # print 2
Any--END--Pattern # print 3
ANY--BEGIN--PATTERN # print 4
ggg # print 5
Any--END--Pattern # print 6
hhh # print 7
Any--END--Pattern # print 8
注释
- 从最新的
ANY--BEGIN--PATTERN
开始打印,在当前的Any--END--Pattern
之前. - 如果没有
ANY--BEGIN--PATTERN
满足,则打印直到最后一个Any--END--Pattern
. - Print from the latest
ANY--BEGIN--PATTERN
before the currentAny--END--Pattern
. - Print until the last
Any--END--Pattern
if noANY--BEGIN--PATTERN
meet. - 单行打印两个图案之间的所有行 [perl]
- 如何在两种模式之间选择行? [awk/sed/grep]
- 仅在两个模式之间的行打印中删除了第一个匹配项
- 如何在两个标记模式之间选择可能会因awk/sed多次出现的行
- 使用awk和可变正则表达式提取两个模式之间的线
- 如何在两个标记模式之间选择可能会因awk/sed多次出现的行
- ...
- One-liner to print all lines between two patterns [perl]
- How to select lines between two patterns? [awk/sed/grep]
- awk print only lines between two patterns removing first match
- How to select lines between two marker patterns which may occur multiple times with awk/sed
- Extract lines between two patterns with awk and a variable regex
- How to select lines between two marker patterns which may occur multiple times with awk/sed
- ...
Notes
我从这些问题中测得的答案将打印ccc
行和/或iii
行...或者不打印具有BEGIN
和END
模式的行.我的几次尝试都具有这些相同的缺点和缺陷.
The answers I have tested from these questions print the line ccc
and/or the line iii
... or do not print the lines having the BEGIN
and END
patterns. My several attempts have these same drawbacks and defects.
我们可以编写一个十行的脚本,但是我敢肯定有一个优雅的单行命令可以解决此问题,但是我找不到它.因此,我认为这可能是一个很好的SO问题;-)
We could write a ten lines script, but I am sure there is an elegant one-line command solving this issue but I cannot find it. Therefore I think this could be a good SO question ;-)
我想知道从sed
,awk
,perl
或类似Unix的系统上易于使用的任何其他工具中使用什么技巧.请使用以下命令提供一个简短的命令行: bash , grep , sed , awk , perl 或您认为的其他任何工具...
I wonder what are the tricks to use from sed
, awk
, perl
or any other tool available easy on our Unix-like systems. Please provide a tiny command line using : bash, grep, sed, awk, perl or any other tool you think...
只需在 Sundeep 的注释中强调非常简单的命令行即可,该注释通过反转输入文件来简化问题:
Just to underline the pretty simple command line from Sundeep's comment that simplifies the problem by reversing the input file:
tac input.txt | sed -n '/END/,/BEGIN/p' | tac
但是此命令行也会打印开头
But this command line also prints the beginning
aaa
Any--END--Pattern
ANY--BEGIN--PATTERN # print 1
ddd # print 2
Any--END--Pattern # print 3
ANY--BEGIN--PATTERN # print 4
ggg # print 5
Any--END--Pattern # print 6
hhh # print 7
Any--END--Pattern # print 8
推荐答案
awk
进行救援!
$ awk '/BEGIN/{c=0; b=1}
{a[c++]=$0}
b&&/END/{for(i=0;i<c;i++) print a[i]; delete a; c=0}' file
ANY--BEGIN--PATTERN # print 1
ddd # print 2
Any--END--Pattern # print 3
ANY--BEGIN--PATTERN # print 4
ggg # print 5
Any--END--Pattern # print 6
hhh # print 7
Any--END--Pattern # print 8
这篇关于打印两个图案(BEGIN和END)之间的最小线条集,包括这两个图案的线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!