本文介绍了打印两个图案(BEGIN和END)之间的最小线条集,包括这两个图案的线条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.
  • Notes

    • Print from the latest ANY--BEGIN--PATTERN before the current Any--END--Pattern.
    • Print until the last Any--END--Pattern if no ANY--BEGIN--PATTERN meet.
      • 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
      • ...

      我从这些问题中测得的答案将打印ccc行和/或iii行...或者不打印具有BEGINEND模式的行.我的几次尝试都具有这些相同的缺点和缺陷.

      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 ;-)

      我想知道从sedawkperl或类似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)之间的最小线条集,包括这两个图案的线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:08