Search code examples
pythoncsvprettytable

Convert Python Pretty table to CSV using shell or batch command line


Whats an easy way convert the output of Python Pretty table to grammatically usable format such as CSV.

The output looks like this :

C:\test> nova list

    spu+--------------------------------------+--------+--------+------------+-------------+-----------------------------------+
     | ID                                   | Name   | Status | Task State | Power State | Networks                          |
     +--------------------------------------+--------+--------+------------+-------------+-----------------------------------+
     | 6bca09f8-a320-44d4-a11f-647dcec0aaa1 | tester | ACTIVE | -          |  Running     | OpenStack-net=10.0.0.1, 10.0.0.3 |
     +--------------------------------------+--------+--------+------------+-------------+-----------------------------------+

Solution

  • Perhaps this will get you close:

    nova list | grep -v '\-\-\-\-' | sed 's/^[^|]\+|//g' | sed 's/|\(.\)/,\1/g' | tr '|' '\n'
    

    This will strip the --- lines Remove the leading | Replace all but the last | with , Replace the last | with \n