awk 仅将特定行转置为多列

awk only specific rows transpose into multiple columns

有人知道如何转置文件中的行输入吗?

invStatus: ONLINE
System: 55
StatFail: 0
invState: 0 Unknown
invFailReason: None
invBase: SYS-MG5-L359-XO1-TRAFFIC STAT 5: TRAF2
invFlag: 0xeee5 SEMAN PRESENT STATUS H_DOWN BASE LOGIC_ONLINE DEX EPASUS INDEX ACK
dexIn: 0
dexIO: 0
badTrans: 0
badSys: 0
io_IN: 0
io_OUT: 0
Tr_in: 0
Tr_out: 0

进入类似的输出:

invBase:   SYS-MG5-L359-XO1-TRAFFIC STAT 5: TRAF2
invFlag:   0xeee5 SEMAN PRESENT STATUS H_DOWN BASE LOGIC_ONLINE DEX EPASUS INDEX ACK
invStatus: ONLINE   System:     55    StatFail:   0     invState:    0 Unknown  invFailReason: None   
dexIn:        0     dexIO:       0    badTrans    0     badSys:      0
io_IN:        0     io_OUT:      0    Tr_in:      0     Tr_out:      0

我第一次尝试在开始时在每一行的末尾添加“;”然后加入多行 > 然后根据字符串拆分它们但仍然得到混乱的输出

我正处于格式化阶段:

cat port | sed 's/$/;/g' | awk 'ORS=/;$/?" ":"\n"'

我会从这个开始

awk -F: '
    {data[] = [=10=]}
    END {
        OFS="\t"
        print data["invBase"]
        print data["invFlag"]
        print data["invStatus"], data["System"], data["StatFail"], data["invState"], data["invFailReason"]
        print data["dexIn"], data["dexIO"], data["badTrans"], data["badSys"]
        print data["io_IN"], data["io_OUT"], data["Tr_in"], data["Tr_out"]
    }
' file
invBase: SYS-MG5-L359-XO1-TRAFFIC STAT 5: TRAF2
invFlag: 0xeee5 SEMAN PRESENT STATUS H_DOWN BASE LOGIC_ONLINE DEX EPASUS INDEX ACK
invStatus: ONLINE       System: 55      StatFail: 0     invState: 0 Unknown     invFailReason: None
dexIn: 0        dexIO: 0        badTrans: 0     badSys: 0
io_IN: 0        io_OUT: 0       Tr_in: 0        Tr_out: 0

然后,为了让它尽可能漂亮,从存储行长度开始,并使用其中一些长度将 print 语句更改为 printf 语句。


仔细查看文件发现,除了 3 行外,它们是连续的,可以粘贴到 4 列中:

awk -F: '
     == "invBase" ||  == "invFlag" {print; next}
     == "invStatus" {invStatus = [=12=]; next}
    {line[n++] = [=12=]}
    END {
        printf invStatus "\t"
        paste = "paste - - - -"
        for (i=0; i<n; i++) {print line[i] | paste}
        close(paste)
    }
' file

提供与上面相同的输出。