3

我正在尝试使用 awk 读取文件,并且仅显示不以 + 或 - 连续 4 次或更多次开头的行。gawk 也可以。每个分组由一个空行分隔。

这是文件中的一个示例,这些是我不想打印的

+Host is up.
+Not shown: 95 closed ports, 3 filtered ports
+PORT     STATE SERVICE   VERSION
+23/tcp   open  telnet
+9100/tcp open  jetdirect

-Host is up.
-Not shown: 99 closed ports
-PORT     STATE SERVICE VERSION
-5900/tcp open  vnc

想要打印的文件样本(不是连续 4 个或更多):

-Not shown: 76 closed ports, 18 filtered ports
+Not shown: 93 closed ports
PORT    STATE SERVICE VERSION
+514/tcp open  shell

我正在学习如何使用 awk,因为我一直在阅读 O'Reilly 的 awk & sed 但我对这个问题有点困惑。此外,如果有人愿意,我不介意使用 shell 脚本解决这个问题的非 awk 方法。

谢谢!

4

2 回答 2

5

如果我理解你的问题,输入文件的记录是段落,所以你需要用空行分隔它们。我假设它用于下一个脚本:

内容script.awk

BEGIN {
        ## Separate records by one or more blank lines.
        RS = ""

        ## Each line will be one field. Both for input and output.
        FS = OFS = "\n"
}

## For every paragraph...
{
        ## Flag to check if I will print the paragraph to output.
        ## If 1, print.
        ## If 0, don't print.
        output = 1

        ## Count how many consecutive rows have '+' or '-' as first
        ## character.
        j = 0

        ## Traverse all rows.
        for ( i = 1; i <= NF; i++ ) {
                if ( substr( $i, 1, 1 ) ~ /+|-/ ) {
                        ++j;
                }
                else {
                        j = 0
                }

                if ( j >= 4 ) {
                        output = 0
                        break
                }
        }

        if ( output == 1 ) {
                print $0 "\n"
        }
}

假设以下测试输入文件为infile

+Host is up. 
+Not shown: 95 closed ports, 3 filtered ports
+PORT     STATE SERVICE   VERSION

+Host is up. 
+Not shown: 95 closed ports, 3 filtered ports
+PORT     STATE SERVICE   VERSION
+23/tcp   open  telnet
+9100/tcp open  jetdirect

-Host is up. 
-Not shown: 99 closed ports
-PORT     STATE SERVICE VERSION
-5900/tcp open  vnc 

-Not shown: 76 closed ports, 18 filtered ports
+Not shown: 93 closed ports
PORT    STATE SERVICE VERSION
+514/tcp open  shell

像这样运行脚本:

awk -f script.awk infile

使用以下输出(第一个记录,因为它没有达到四个连续的行,第二个记录,因为它们之间有不同的行):

+Host is up.
+Not shown: 95 closed ports, 3 filtered ports
+PORT     STATE SERVICE   VERSION

-Not shown: 76 closed ports, 18 filtered ports
+Not shown: 93 closed ports
PORT    STATE SERVICE VERSION
+514/tcp open  shell
于 2012-07-16T17:23:19.530 回答
-1
awk '{if(NF>3 &&( $0 ~ /\+/ || $0 ~ /-/) ) print $0}' test.txt
于 2012-07-16T16:36:43.100 回答