两个图案之间提取线从文件

两个图案之间提取线从文件

本文介绍了两个图案之间提取线从文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一个巨大的文件中提取两个匹配模式之间的特定线路。

I need to extract particular lines between two matching patterns from a huge file.

比方说,样式1 (在文件中是唯一的)相匹配的特定行#N PATTERN2 (而不是在文件中是唯一的)匹配行#m的旁边立即赛后行#N 。然后我想提取所有线之间并包括行#N至#M

Let's say pattern1 (unique in a file) matches a particular line # n and pattern2 (not unique in a file) matches line # m next immediate match after line # n. Then I want to extract all lines between and including line #n to #m

示例文件的内容

***************************************************************************
text line # n-2
text line # n-1
********************************* Results *********************************
SUCCEEDED
...
...
some text
***************************************************************************
text line # m+1
text line # m+2
***************************************************************************

所需的输出

********************************* Results *********************************
SUCCEEDED
...
...
some text
***************************************************************************

这将是AP preciated如果你能帮助我解决这个问题。

It would be appreciated if you could help me solve this problem

推荐答案

这可能是一种方法:

$ awk '/pattern1/ {p=1}; p; /pattern2/ {p=0}' file
********************************* Results *********************************
SUCCEEDED
...
...
some text
***************************************************************************


  • 当它找到样式1 ,然后进行变量p = 1。

  • 它只是打印线条时点== 1 。这与 P 的条件来完成。如果这是真的,它执行默认的awk动作,即打印$ 1,0 。否则,它没有。

  • 当它找到 PATTERN2 ,然后进行变量p = 0。由于这种情况后, P 检查情况,将打印在 PATTERN2 出现的第一次就行了。

    • When it finds pattern1, then makes variable p=1.
    • it just prints lines when p==1. This is accomplished with the p condition. If it is true, it performs the default awk action, that is, print $0. Otherwise, it does not.
    • When it finds pattern2, then makes variable p=0. As this condition is checked after p condition, it will print the line in which pattern2 appears for the first time.
    • 如果您想要线的精确匹配:

      If you want an exact match of the lines:

      $ awk '$0=="pattern1" {p=1}; p; $0=="pattern2" {p=0}' file
      

      测试

      $ cat a
      ***************************************************************************
      text line # n-2
      pattern1
      ********************************* Results *********************************
      SUCCEEDED
      ...
      ...
      some text
      ***************************************************************************
      pattern2
      text line # m+2
      pattern2
      ***************************************************************************
      $ awk '/pattern1/ {p=1}; p; /pattern2/ {p=0}' a
      pattern1
      ********************************* Results *********************************
      SUCCEEDED
      ...
      ...
      some text
      ***************************************************************************
      pattern2
      

      这篇关于两个图案之间提取线从文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:38