如何在 bash 终端中设置正确的列?

如何在 bash 终端中设置正确的列?

我正在尝试为我的代码输出定义适当的列。问题是,如果列的元素之一为空/不在字符限制内,则后续的列元素会被替换。

VULDB-ID        LEVEL   CVE             TITLE

122215          LOW     CVE-2015-9261   BusyBox up to 1.27.1 ZIP File decompress_gunzip.c null pointer dereference
119934          MEDIUM  CVE-2018-1000517        BusyBox wget memory corruption
119917          MEDIUM  CVE-2018-1000500        BusyBox SSL Certificate Validator certificate validation
96755           LOW     CVE-2016-2147   BusyBox up to 1.24.x DHCP Client integer overflow
94808           MEDIUM  null    BusyBox up to 1.23.1/1.4.x cmdline arp_main IFNAME memory corruption
93993           MEDIUM  CVE-2016-6301   BusyBox NTP Packet networking/ntpd.c recv_and_process_client_pkt resource management

我正在使用以下代码来输出该表

title=$(jq -r .entry.title result.json 2>/dev/null)
cve=$(jq -r .source.cve.id result.json 2>/dev/null)
id=$(jq -r .entry.id result.json 2>/dev/null)
level=$(jq -r .vulnerability.risk.name result.json 1> level.tmp 2> /dev/null)    

paste <(tput smul; printf %s"VULDB-ID"; tput rmul) <(tput smul; printf %s"LEVEL"; tput rmul) <(tput smul; printf %s"CVE"; tput rmul;) <(echo) <(tput smul; printf %s"TITLE"; tput rmul)
paste <(echo "$id") <(echo) <(cat level.tmp) <(echo "$cve") <(echo "$title") | column -t -s "\"" -L

数据是从以下 json 文件中提取的。

{
    "entry": {
        "id": "122215",
        "title": "BusyBox up to 1.27.1 ZIP File decompress_gunzip.c null pointer dereference",
        "timestamp": {
            "create": "1532670117",
            "change": "1583845511"
        }
    },
    "vulnerability": {
        "risk": {
            "value": "1",
            "name": "low"
        }
    },
    "advisory": {
        "date": "1532556000"
    },
    "source": {
        "cve": {
            "id": "CVE-2015-9261"
        }
    }
},

{
    "entry": {
        "id": "119934",
        "title": "BusyBox wget memory corruption",
        "timestamp": {
            "create": "1530081876",
            "change": "1582365028"
        }
    },
    "vulnerability": {
        "risk": {
            "value": "2",
            "name": "medium"
        }
    },
    "advisory": {
        "date": "1529971200"
    },
    "source": {
        "cve": {
            "id": "CVE-2018-1000517"
        }
    }
},

{
    "entry": {
        "id": "119917",
        "title": "BusyBox SSL Certificate Validator certificate validation",
        "timestamp": {
            "create": "1530080858",
            "change": "1582358574"
        }
    },
    "vulnerability": {
        "risk": {
            "value": "2",
            "name": "medium"
        }
    },
    "advisory": {
        "date": "1529971200"
    },
    "source": {
        "cve": {
            "id": "CVE-2018-1000500"
        }
    }
},

{
    "entry": {
        "id": "96755",
        "title": "BusyBox up to 1.24.x DHCP Client integer overflow",
        "timestamp": {
            "create": "1486665130",
            "change": "1597250919"
        }
    },
    "vulnerability": {
        "risk": {
            "value": "1",
            "name": "low"
        }
    },
    "advisory": {
        "date": "1486598400"
    },
    "source": {
        "cve": {
            "id": "CVE-2016-2147"
        }
    }
},

{
    "entry": {
        "id": "94808",
        "title": "BusyBox up to 1.23.1\/1.4.x cmdline arp_main IFNAME memory corruption",
        "timestamp": {
            "create": "1483348691",
            "change": "1564047629"
        }
    },
    "vulnerability": {
        "risk": {
            "value": "2",
            "name": "medium"
        }
    },
    "advisory": {
        "date": "1483228800"
    }
},

{
    "entry": {
        "id": "93993",
        "title": "BusyBox NTP Packet networking\/ntpd.c recv_and_process_client_pkt resource management",
        "timestamp": {
            "create": "1481373506",
            "change": "1561216351"
        }
    },
    "vulnerability": {
        "risk": {
            "value": "1",
            "name": "medium"
        }
    },
    "advisory": {
        "date": "1481241600"
    },
    "source": {
        "cve": {
            "id": "CVE-2016-6301"
        }
    }
},

现在我的问题是

  1. 我必须定义自己的列吗?如果是,那么我该怎么做呢?
  2. 有没有一种更简单、更优雅的方法来漂亮地打印我所缺少的列?

答案1

没关系,我找到了解决方案。我使用“制表符”作为分隔符,如下所示

 paste <(echo "$id") <(cat level.tmp) <(echo "$cve") <(echo "$title") | column -t -s$'\t' -L --table-columns ${vulid},${level},${cveid},${titleout}
        

这给出了以下输出。

VULDB-ID  LEVEL     CVE                 TITLE
122215    LOW       CVE-2015-9261       BusyBox up to 1.27.1 ZIP File decompress_gunzip.c null pointer dereference
119934    MEDIUM    CVE-2018-1000517    BusyBox wget memory corruption
119917    MEDIUM    CVE-2018-1000500    BusyBox SSL Certificate Validator certificate validation
96755     LOW       CVE-2016-2147       BusyBox up to 1.24.x DHCP Client integer overflow
94808     MEDIUM    null                BusyBox up to 1.23.1/1.4.x cmdline arp_main IFNAME memory corruption
93993     MEDIUM    CVE-2016-6301       BusyBox NTP Packet networking/ntpd.c recv_and_process_client_pkt resource management

我现在唯一的疑问是,在未来的情况下,这种方法可能会在输出中造成问题吗?有没有更优雅的方法来做到这一点?

相关内容