将stdout块解析为bash或ruby中的数组

问题描述 投票:0回答:1

我正在尝试找到将racadm(戴尔机箱/ idrac)中的stdout日志条目转换为单独的数组或json数组的最有效方法,以便我可以一次评估每个条目。输出始终具有相同的字段。下面的输出是非常典型的

$ racadm chassislog view -c Storage -b PDR
SeqNumber       = 11700
Message ID      = PDR17
Category        = Storage
AgentID         = CMC
Severity        = Information
Timestamp       = 2020-03-21 00:02:06
Message Arg   1 = Physical Disk 0:0:15
FQDD            = Disk.Bay.15:Enclosure.Internal.0-0:RAID.ChassisIntegrated.1-1
Message         = Global hot spare assigned to Physical Disk 0:0:15.
--------------------------------------------------------------------------------
SeqNumber       = 11699
Message ID      = PDR26
Category        = Storage
AgentID         = CMC
Severity        = Information
Timestamp       = 2020-03-21 00:02:04
Message Arg   1 = Physical Disk 0:0:3
FQDD            = Disk.Bay.3:Enclosure.Internal.0-0:RAID.ChassisIntegrated.1-1
Message         = Physical Disk 0:0:3 is online.
--------------------------------------------------------------------------------
SeqNumber       = 11696
Message ID      = PDR71
Category        = Storage
AgentID         = CMC
Severity        = Information
Timestamp       = 2020-03-21 00:02:01
Message Arg   1 = Physical Disk 0:0:15
Message Arg   2 = Physical Disk 0:0:3
FQDD            = Disk.Bay.15:Enclosure.Internal.0-0:RAID.ChassisIntegrated.1-1
Message         = Copyback completed from Physical Disk 0:0:15 to Physical Disk 0:0:3.
--------------------------------------------------------------------------------
SeqNumber       = 11670
Message ID      = PDR70
Category        = Storage
AgentID         = CMC
Severity        = Information
Timestamp       = 2020-03-20 21:45:47
Message Arg   1 = Physical Disk 0:0:15
Message Arg   2 = Physical Disk 0:0:3
FQDD            = Disk.Bay.15:Enclosure.Internal.0-0:RAID.ChassisIntegrated.1-1
Message         = Copyback started from Physical Disk 0:0:15 to Physical Disk 0:0:3.
--------------------------------------------------------------------------------
SeqNumber       = 11667
Message ID      = PDR8
Category        = Storage
AgentID         = CMC
Severity        = Information
Timestamp       = 2020-03-20 21:45:44
Message Arg   1 = Physical Disk 0:0:3
FQDD            = Disk.Bay.3:Enclosure.Internal.0-0:RAID.ChassisIntegrated.1-1
Message         = Physical Disk 0:0:3 is inserted.
--------------------------------------------------------------------------------

我真的很想将整个输出读入一个关联数组,这样我就可以逐步完成for事件循环中的每个条目。寻找红宝石(厨师)或bash的指导。

arrays ruby bash parsing stdout
1个回答
0
投票

不是bash,因为shell用于处理文件和启动命令,但是使用GNU awk(通常被误认为是shell的一部分),它是一种简单而强大的编程语言。 逐步进入事件的for循环中的每个条目并不是真正的要求,因此这里是一个小示例:

$ gawk -v item="Message Arg   2" '  # queried item as parameter 
BEGIN {
    RS="\n-+$\n"                    # record is separated by a bunch of -:s
    FS="\n"                         # a line is a field within a record
}
{
    for(nf=1;nf<=NF;nf++) {         # loop all lines in a record
        split($nf,t,/ *= */)        # split lines by = and surrounding space
        a[NR][t[1]]=t[2]            # hash to a 2 dimensional array indexed by
    }                               # record no. and the item, value as value
}
END {                               # after lines are hashed, make queries
    for(nr in a)                    # for each record in hash
        if(item in a[nr])           # if queried item is found in it
            printf "%d: %s = %s\n", nr,item,a[nr][item]  # output
}' file

查询项目Message Arg 2的输出:

3: Message Arg   2 = Physical Disk 0:0:3
4: Message Arg   2 = Physical Disk 0:0:3
© www.soinside.com 2019 - 2024. All rights reserved.