Suppress new line character when displaying "lxc list" output

Running LXC 2.18 and have a number of containers running with multiple IPs. I am writing a script to push the data into redis via the “lxc list -cn4scl,image.os,image.release” command. Unfortunately, the output gives a new-line break if the container is running multiple IPs. I have tried all variants of sed, awk, tr to keep the output from wrapping to a new line but have not found the magic yet.

Any pointers?

ron@LXD-QA-Server-01# lxc list -cn4scl,image.os,image.release
+--------------------+--------------------------------+---------+----------------------+----------------------+----------+---------------+
|        NAME        |              IPV4              |  STATE  |      CREATED AT      |     LAST USED AT     | IMAGE OS | IMAGE RELEASE |
+--------------------+--------------------------------+---------+----------------------+----------------------+----------+---------------+
| ron-firewall       | 192.168.108.1 (eth1)           | RUNNING | 2017/08/20 21:39 UTC | 2017/08/21 00:40 UTC |          |               |
|                    | 172.16.11.201 (eth0)           |         |                      |                      |          |               |
+--------------------+--------------------------------+---------+----------------------+----------------------+----------+---------------+
| ron-test-server-1  | 192.168.108.11 (eth0)          | RUNNING | 2017/08/21 01:28 UTC | 2017/08/21 01:30 UTC |          |               |
+--------------------+--------------------------------+---------+----------------------+----------------------+----------+---------------+
| ron-test-server-2  | 192.168.109.11 (eth0)          | RUNNING | 2017/09/01 01:42 UTC | 2017/09/01 01:43 UTC |          |               |
+--------------------+--------------------------------+---------+----------------------+----------------------+----------+---------------+
| bob-firewall       | 192.168.109.1 (eth1)           | RUNNING | 2017/08/21 20:05 UTC | 2017/08/21 21:00 UTC |          |               |
|                    | 172.16.11.202 (eth0)           |         |                      |                      |          |               |
+--------------------+--------------------------------+---------+----------------------+----------------------+----------+---------------+

ron@LXD-QA-Server-01# lxc list -cn4scl,image.os,image.release --format=csv
ron-firewall,"192.168.108.1 (eth1)
172.16.11.201 (eth0)",RUNNING,2017/08/20 21:39 UTC,2017/08/21 00:40 UTC,,
ron-test-server-1,192.168.108.11 (eth0),RUNNING,2017/08/21 01:28 UTC,2017/08/21 01:30 UTC,,
ron-test-server-2,192.168.108.12 (eth0),RUNNING,2017/08/21 01:28 UTC,2017/08/21 01:30 UTC,,
bob-firewall,"192.168.109.1 (eth1)
172.16.11.202 (eth0)",RUNNING,2017/08/20 21:39 UTC,2017/08/21 00:40 UTC,,

What information does your script need exactly?

I suspect that “lxc list --format=json” combined with “jq” would probably be an easier and more reliable way to get exactly what you want out of it.

Right now, I am looking for the output from "lxc list -cn4scl,image.os,image.release”. However, any key I can pull from the lxc list command would be helpful. I tried using the “jq” command from a json format, but I could not understand how to get the data. For me, the best output is csv as this is the way I add elements to the Redis database.

Can you please give me a working example of "format=json | jq” I can use to query the network IPv4 addresses w/out line breaks? Also, how can I get the csv output from adding a line break for any/all items?

Thanks!

-Ron

One way to do it with jq would be something like:

stgraber@castiana:~$ lxc list --format=json | jq -r '.[] | select(.state != null) | "\(.name) \(.state.network.eth0.addresses | .[].address)"'
android-dev 10.204.119.108
android-dev 2001:470:b368:4242:216:3eff:fe13:8d9
android-dev fe80::216:3eff:fe13:8d9
openhab 10.204.119.76
openhab 2001:470:b368:4242:216:3eff:fee2:d7a2
openhab fe80::216:3eff:fee2:d7a2
snapcraft 10.204.119.120
snapcraft 2001:470:b368:4242:216:3eff:febe:f187
snapcraft fe80::216:3eff:febe:f187
ups-monitor 10.204.119.41
ups-monitor 2001:470:b368:4242:216:3eff:fe1f:c67a
ups-monitor fe80::216:3eff:fe1f:c67a