Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash:如何从文本文件中提取类似表的结构_Bash_Awk_Sed - Fatal编程技术网

Bash:如何从文本文件中提取类似表的结构

Bash:如何从文本文件中提取类似表的结构,bash,awk,sed,Bash,Awk,Sed,我有一个日志文件,其中包含一些数据和重要的表格部分,如下所示: //Some data -------------------------------------------------------------------------------- ----- Output Table ----- ---------------------------------------

我有一个日志文件,其中包含一些数据和重要的表格部分,如下所示:

    //Some data

    --------------------------------------------------------------------------------
    -----                 Output Table                             -----
    --------------------------------------------------------------------------------
            NAME                         Attr1    Attr2      Attr3    Attr4    Attr5
    --------------------------------------------------------------------------------
    fooooooooo                               0        0          3        0        0
    boooooooooooooooooooooo                  0        0         30        0        0
    abv                                      0        0         16        0        0
    bhbhbhbh                                 0        0          3        0        0
    foooo                                    0        0        198        0        0

    WARNING: Some message...


    WARNING: Some message...

    aaaaaaaaa                                0        0         60        0        7
    bbbbbbbb                                 0        0         48        0        7
    ccccccc                                  0        0         45        0        7
    rrrrrrr                                  0        0         50        0        7
    abcabca                                  0        0         42        0        6

// Some data...

    --------------------------------------------------------------------------------
    -----                 Another Output Table                                 -----
    --------------------------------------------------------------------------------
         NAME                            Attr1    Attr2      Attr3    Attr4    Attr5
    --------------------------------------------------------------------------------
    $$foo12                                  0        0          3        0        0
    $$foo12_720_720_14_2                     0        0         30        0        0
我想从给定的文件中提取所有这类表,并保存在单独的文件中

注意事项:

  • 表的开头表示包含{NAME,Attr1,…,Attr5}个单词的行
  • 警告消息可能存在于表的范围内,应忽略
  • 当出现空行且该空行的下一行不是“警告”行时,表结束
因此,我期望以下两个文件作为输出:

        NAME                         Attr1    Attr2      Attr3    Attr4    Attr5
--------------------------------------------------------------------------------
fooooooooo                               0        0          3        0        0
boooooooooooooooooooooo                  0        0         30        0        0
abv                                      0        0         16        0        0
bhbhbhbh                                 0        0          3        0        0
foooo                                    0        0        198        0        0
aaaaaaaaa                                0        0         60        0        7
bbbbbbbb                                 0        0         48        0        7
ccccccc                                  0        0         45        0        7
rrrrrrr                                  0        0         50        0        7
abcabca                                  0        0         42        0        6


我将按照您的指示编写以下awk脚本

#! /usr/bin/awk -f

# start a table with a NAME line
/^ +NAME/ {
    titles = $0
    print
    next
}

# don't print if not in table
! titles {
    next
}

# blank line may mean end-of-table
/^$/ {
    EOT = 1
    next
}

# warning is not EOT
/^WARNING/ {
    EOT = 0
    next
}

# end of table means we're not in a table anymore, Toto
EOT {
    titles = 0
    EOT = 0
    next
}

# print what's in the table
{ print }
试试这个-

awk -F'[[:space:]]+' 'NF>6 || ($0 ~ /-/ && $0 !~ "Output") {print $0}' f
    --------------------------------------------------------------------------------
    --------------------------------------------------------------------------------
            NAME                         Attr1    Attr2      Attr3    Attr4    Attr5
    --------------------------------------------------------------------------------
    fooooooooo                               0        0          3        0        0
    boooooooooooooooooooooo                  0        0         30        0        0
    abv                                      0        0         16        0        0
    bhbhbhbh                                 0        0          3        0        0
    foooo                                    0        0        198        0        0
    aaaaaaaaa                                0        0         60        0        7
    bbbbbbbb                                 0        0         48        0        7
    ccccccc                                  0        0         45        0        7
    rrrrrrr                                  0        0         50        0        7
    abcabca                                  0        0         42        0        6
    --------------------------------------------------------------------------------
    --------------------------------------------------------------------------------
         NAME                            Attr1    Attr2      Attr3    Attr4    Attr5
    --------------------------------------------------------------------------------
    $$foo12                                  0        0          3        0        0
    $$foo12_720_720_14_2                     0        0         30        0        0

我投票结束这个问题,因为它似乎是一个对工具或解决方案的建议请求,而不是对您自己的代码的帮助请求。这使您的问题脱离StackOverflow的主题。如果这个评估是错误的,并且你确实需要帮助编写你自己的代码,那么请允许我收回我的投票。
awk -F'[[:space:]]+' 'NF>6 || ($0 ~ /-/ && $0 !~ "Output") {print $0}' f
    --------------------------------------------------------------------------------
    --------------------------------------------------------------------------------
            NAME                         Attr1    Attr2      Attr3    Attr4    Attr5
    --------------------------------------------------------------------------------
    fooooooooo                               0        0          3        0        0
    boooooooooooooooooooooo                  0        0         30        0        0
    abv                                      0        0         16        0        0
    bhbhbhbh                                 0        0          3        0        0
    foooo                                    0        0        198        0        0
    aaaaaaaaa                                0        0         60        0        7
    bbbbbbbb                                 0        0         48        0        7
    ccccccc                                  0        0         45        0        7
    rrrrrrr                                  0        0         50        0        7
    abcabca                                  0        0         42        0        6
    --------------------------------------------------------------------------------
    --------------------------------------------------------------------------------
         NAME                            Attr1    Attr2      Attr3    Attr4    Attr5
    --------------------------------------------------------------------------------
    $$foo12                                  0        0          3        0        0
    $$foo12_720_720_14_2                     0        0         30        0        0