如何选择两行之间的行

时间:2017-03-14 03:50:50

标签: awk sed

我有一个包含某些命令输出的文件。

# cat input.txt

*******************************************************************************
deinstall PREVIEW:  deinstall operation will not actually occur.
*******************************************************************************

+-----------------------------------------------------------------------------+
                    Pre-deinstall Verification...
+-----------------------------------------------------------------------------+
Verifying selections...done
Verifying requisites...done
Results...

FAILURES
--------
  Filesets listed in this section failed pre-deinstall verification
  and will not be removed.

  Dependency Failures
  (Deinstall Operation)
  ---------------------
  SELECTED FILESETS:  The following is a list of filesets that you asked to
  remove.  They cannot be removed until all of their dependent filesets
  are also removed.  See subsequent lists for details of dependents.

    my.data.list 6.1.2.0         # This is my data list...

  INSTALLED DEPENDENTS:  The following filesets are dependents of one or more
  of the selected filesets listed above.  These must be removed before
  or with the filesets that you selected.  To remove these dependents with
  the selected filesets, specify the option to automatically remove dependent
  software (-g flag).

    apple.fruit 6.1.9.200            # This is apple...
    ball.object 6.1.9.200             # This is ball 
    bat.object 6.1.9.200              # This is bat
    cat.animal 6.1.9.200              # this is cat 
    nut.object 6.1.9.200              # this is nut 
    hut.house 6.1.9.200              # this is hut 

  << End of Failure Section >>

FILESET STATISTICS 
------------------
    1  Selected to be deinstalled, of which:
        1  FAILED pre-deinstall verification
  ----
    0  Total to be deinstalled


******************************************************************************
End of deinstall.  No deinstall operation has actually occurred.
******************************************************************************

我正在尝试获取/打印已安装的家属列表

apple.fruit 6.1.9.200            # This is apple...
ball.object 6.1.9.200             # This is ball 
bat.object 6.1.9.200              # This is bat
cat.animal 6.1.9.200              # this is cat 
nut.object 6.1.9.200              # this is nut 
hut.house 6.1.9.200              # this is hut 

我已经浏览了这个How to select lines between two patterns?链接。并提出了这个命令,但它没有用。

我在这里做错了什么?

awk '/"software (-g flag)."/ f; /"<< End of Failure Section >>"/{f=0}' input.txt

7 个答案:

答案 0 :(得分:3)

永远不要只说it is not working,无论是在寻求帮助来调试软件,还是将车开到机械师那里,或是在兽医那里放下你的狗。始终描述有问题的项目的症状“不工作”,以便最有可能获得帮助,例如:输出错误,没有输出,核心转储,错误消息,其他一些(其中一些不适用于狗案:-))。

说完了:

  1. 文件中的开头/结尾行不包含双引号 不要让你的脚本匹配它们,
  2. ().是regexp元字符,所以如果你想要对它们进行字面处理,你必须逃避它们。
  3. 试试这个:

    awk '/software \(-g flag\)\./ f; /<< End of Failure Section >>/{f=0}' input.txt
    

    然后将其修改为:

    awk '/software \(-g flag\)\./ f && NF; /<< End of Failure Section >>/{exit}' input.txt
    

    摆脱空白线条和效率。

答案 1 :(得分:2)

@Albert:试试:

awk '/End of Failure Section/{A=""} /software \(-g flag\)/{A=1;next} A && !/^$/{print}'   Input_file

说明:检查一行是否有字符串(软件(-g标志))如果是,则将名为A的变量设为TRUE,如果一行有字符串(故障结束部分)然后将名为A&#39的变量赋值为0.然后检查变量A的值是否为TRUE / ONE且行是否为NULL然后打印当前行。

答案 2 :(得分:1)

$ awk '
       /software \(-g flag\)/{ f=1; next }
       /<< End of Failure Section >>/{f=0}
       f && NF
      ' file

<强>解释

$ awk '                                       # call awk
       /software \(-g flag\)/{ f=1; next }    # look for regex in record, if found set var f=1 and got to next line
       /<< End of Failure Section >>/{f=0}    # look for regex if found then set var f to 0
       f && NF                                # if f and NF then print record, this is to skip empty lines
      ' file

答案 3 :(得分:1)

以下是awk的另一种解决方案:

awk '/INSTALLED DEP/{a=1} a&&/^$/{b=1;a=0;next} b{print} b&&/^$/{b=0}' File

    apple.fruit 6.1.9.200            # This is apple...
    ball.object 6.1.9.200             # This is ball
    bat.object 6.1.9.200              # This is bat
    cat.animal 6.1.9.200              # this is cat
    nut.object 6.1.9.200              # this is nut
    hut.house 6.1.9.200              # this is hut

答案 4 :(得分:1)

因为有sed标签:

sed -n '/software (-g flag)/,/<< End of Failure Section/{//!p}' input.txt

答案 5 :(得分:1)

sed '/software (-g flag)./,/End of Failure Section/!d;//d' input.txt

答案 6 :(得分:0)

另一种方法是使用正则表达式匹配来打印包含所需模式的记录:

$ awk '/([a-z][. ])+([0-9].?)+/' file
    my.data.list 6.1.2.0         # This is my data list...
    apple.fruit 6.1.9.200            # This is apple...
    ball.object 6.1.9.200             # This is ball 
    bat.object 6.1.9.200              # This is bat
    cat.animal 6.1.9.200              # this is cat 
    nut.object 6.1.9.200              # this is nut 
    hut.house 6.1.9.200              # this is hut 

上面的正则表达式并不完美,例如第一个输出的记录不应该在那里,应该除去例如&& !/my./添加条件:

$ awk '/([a-z][. ])+([0-9].?)+/ && !/my./' file