0

I am trying to define proper columns to my code output. The problem is that if one of column's elements is empty/not within character limit, the subsequent column elements get displaced.

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

I am using the following code to output this table

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

The data is being extracted from the following json file.

{
    "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"
        }
    }
},

Now my question is

  1. Do i have to define my own columns? If yes, then how to i go about doing that?
  2. Is there an easier, more elegant way to pretty print columns that i am missing?
5
  • @thanasisp so if i can somehow define proper delimiters like "|" , can I create a proper column? Commented Dec 7, 2020 at 12:52
  • The data is being extracted from a json file. Should i include that as well? Commented Dec 7, 2020 at 13:01
  • 1
    The JSON you have included is not valid JSON. Please update the question with a valid JSON file.
    – thanasisp
    Commented Dec 8, 2020 at 17:37
  • 1
    This would be easy to do with jq if the JSON document was correctly formatted. It's currently looking as it's missing [ and ] at the start end, and it has a trailing comma at the end and some empty lines. Instead of posting a modified JSON, consider posting the original document without modifications.
    – Kusalananda
    Commented Dec 14, 2020 at 10:54
  • The problem was the original document was very big. I'll try to retrieve the original document and edit the question. Commented Dec 14, 2020 at 13:02

1 Answer 1

0

Nevermind i found a solution. I used "tab" as a delimiter as follows

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

This gives the following output.

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

My only doubt now is that is there a scenario in the future where this method might pose an issue in the output? Is there a more elegant way to do this?

1
  • 1
    jq should be able to do the whole extraction as TSV as well with something like jq -r '.[]|[.entry.id,.vulnerability.risk.name,.source.cve.id,.entry.title]|@tsv' Commented Dec 14, 2020 at 10:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .