Awk 如何在两行之间选择行

Awk 如何在两行之间选择行,awk,sed,Awk,Sed,我有一个包含某个命令输出的文件 # cat input.txt ******************************************************************************* deinstall PREVIEW: deinstall operation will not actually occur. ************************************************************************

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

# 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.
******************************************************************************
我已经浏览了这个链接。并提出了这个命令,但它不起作用

我做错了什么

awk '/"software (-g flag)."/ f; /"<< End of Failure Section >>"/{f=0}' input.txt
@艾伯特:试试看:

awk '/End of Failure Section/{A=""} /software \(-g flag\)/{A=1;next} A && !/^$/{print}'   Input_file
说明:检查一行是否具有字符串软件-g标志如果是,则将名为a的变量设置为TRUE,如果一行具有字符串结束失败部分,则将名为a的变量的值设置为0。然后检查变量A的值是否为TRUE/ONE,以及行是否为NULL,然后打印当前行

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


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

不要只是说它不起作用,无论是在请求帮助调试软件时,还是在把你的车送到修理工那里,或是在把你的狗送到兽医那里时。始终描述相关项目无法获得最佳帮助的症状,例如错误输出、无输出、核心转储、错误消息,以及其他一些不适用于狗的情况:-

尽管如此:

文件中的开始/结束行没有用双引号括起来,因此 不要要求脚本与它们匹配,并且 和都是regexp元字符,所以如果你想按字面意思处理它们,就必须避开它们。 试试这个:

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

去掉空行和效率。

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

$ 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 
上面的正则表达式决不是完美的,例如,第一个输出的记录不应该在那里,应该删除,添加例如&/我的./符合以下条件:

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

因为有一个sed标记:

sed -n '/software (-g flag)/,/<< End of Failure Section/{//!p}' input.txt
$ 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 
$ awk '/([a-z][. ])+([0-9].?)+/ && !/my./' file
sed -n '/software (-g flag)/,/<< End of Failure Section/{//!p}' input.txt
sed '/software (-g flag)./,/End of Failure Section/!d;//d' input.txt