如何从Openstack nova列表中仅获取ID Name和Networks字段

时间:2016-08-20 15:13:29

标签: awk sed openstack openstack-nova

我的新星列表输出为:

            #  nova list
            +--------------------------------------+---------------+--------+------------+-------------+-----------------------------------------------------------------------------------------+
            | ID                                   | Name          | Status | Task State | Power State | Networks                                                                                |
            +--------------------------------------+---------------+--------+------------+-------------+-----------------------------------------------------------------------------------------+
            | 0017050b-62ba-475e-b598-4638d4293250 | VNFM-POC      | ACTIVE | -          | Running     | vnfm_net_1=172.16.10.10, 10.10.72.12                                                    |
            | ff21d7c9-dc92-4256-ae49-49aa54c6a67b | VNFM_Shreyans | ACTIVE | -          | Running     | vnfm_net_1=172.16.10.8, 10.10.72.20                                                     |
            | 3f0a1188-c151-4e5e-9930-969d0423601b | dsc-test-3    | ACTIVE | -          | Running     | dsc-sig=172.16.17.15, 10.10.72.15; dsc-InterInstance=172.16.18.15; dsc-OAM=172.16.16.20 |
            +--------------------------------------+---------------+--------+------------+-------------+-----------------------------------------------------------------------------------------+

我想只提取ID名称网络。

#  nova list | sed 's/ //g'| cut -d'|' -f 2,3,7
+--------------------------------------+---------------+--------+------------+-------------+-----------------------------------------------------------------------------------------+
ID|Name|Networks
+--------------------------------------+---------------+--------+------------+-------------+-----------------------------------------------------------------------------------------+
0017050b-62ba-475e-b598-4638d4293250|VNFM-POC|vnfm_net_1=172.16.10.10,10.10.72.12
ff21d7c9-dc92-4256-ae49-49aa54c6a67b|VNFM_Shreyans|vnfm_net_1=172.16.10.8,10.10.72.20
3f0a1188-c151-4e5e-9930-969d0423601b|dsc-test-3|dsc-sig=172.16.17.15,10.10.72.15;dsc-InterInstance=172.16.18.15;dsc-OAM=172.16.16.20
+--------------------------------------+---------------+--------+------------+-------------+-----------------------------------------------------------------------------------------+

问题: 我的表格式搞砸了。如何只获取ID Name和Networks字段,如下所示。

+--------------------------------------+---------------+-----------------------------------------------------------------------------------------+
| ID                                   | Name          | Networks                                                                                   |
+--------------------------------------+---------------+-----------------------------------------------------------------------------------------+
| 0017050b-62ba-475e-b598-4638d4293250 | VNFM-POC      | vnfm_net_1=172.16.10.10, 10.10.72.12                                                    |
| ff21d7c9-dc92-4256-ae49-49aa54c6a67b | VNFM_Shreyans | vnfm_net_1=172.16.10.8, 10.10.72.20                                                     |
| 3f0a1188-c151-4e5e-9930-969d0423601b | dsc-test-3    | dsc-sig=172.16.17.15, 10.10.72.15; dsc-InterInstance=172.16.18.15; dsc-OAM=172.16.16.20 |
+--------------------------------------+---------------+-----------------------------------------------------------------------------------------+

OR

ID          0017050b-62ba-475e-b598-4638d4293250
Name        VNFM-POC
Networks    vnfm_net_1=172.16.10.10,10.10.72.12

ID          ff21d7c9-dc92-4256-ae49-49aa54c6a67b
Name        VNFM_Shreyans
Networks    vnfm_net_1=172.16.10.8,10.10.72.20

ID          3f0a1188-c151-4e5e-9930-969d0423601b
Name        dsc-test-3
Networks    dsc-sig=172.16.17.15,10.10.72.15;dsc-InterInstance=172.16.18.15;dsc-OAM=172.16.16.20

3 个答案:

答案 0 :(得分:2)

$ nova list | awk -F'|' '/\|/ && !/ID/{print "ID\t"$2"\nName\t"$3"\nNetworks"$7"\n"}'
ID   0017050b-62ba-475e-b598-4638d4293250 
Name     VNFM-POC      
Networks vnfm_net_1=172.16.10.10, 10.10.72.12                                                    

ID   ff21d7c9-dc92-4256-ae49-49aa54c6a67b 
Name     VNFM_Shreyans 
Networks vnfm_net_1=172.16.10.8, 10.10.72.20                                                     

ID   3f0a1188-c151-4e5e-9930-969d0423601b 
Name     dsc-test-3    
Networks dsc-sig=172.16.17.15, 10.10.72.15; dsc-InterInstance=172.16.18.15; dsc-OAM=172.16.16.20 

请注意,上面的命令在文件末尾会有额外的空行...

我能用其他格式做的最好的事情是:

$ nova list | awk -F'|' '/\|/{print "|"$1"|"$2"|"$3"|"$7"|"} /+-/{print}'
            +--------------------------------------+---------------+--------+------------+-------------+-----------------------------------------------------------------------------------------+
|            | ID                                   | Name          | Networks                                                                                |
            +--------------------------------------+---------------+--------+------------+-------------+-----------------------------------------------------------------------------------------+
|            | 0017050b-62ba-475e-b598-4638d4293250 | VNFM-POC      | vnfm_net_1=172.16.10.10, 10.10.72.12                                                    |
|            | ff21d7c9-dc92-4256-ae49-49aa54c6a67b | VNFM_Shreyans | vnfm_net_1=172.16.10.8, 10.10.72.20                                                     |
|            | 3f0a1188-c151-4e5e-9930-969d0423601b | dsc-test-3    | dsc-sig=172.16.17.15, 10.10.72.15; dsc-InterInstance=172.16.18.15; dsc-OAM=172.16.16.20 |
            +--------------------------------------+---------------+--------+------------+-------------+-----------------------------------------------------------------------------------------+

答案 1 :(得分:1)

最简单的方法是使用--fields,如下所示:

$ nova list --fields name,networks
+--------------------------------------+--------------------------------------------+--------------------------------------------------------------+
| ID                                   | Name                                       | Networks                                                     |
+--------------------------------------+--------------------------------------------+--------------------------------------------------------------+
| 1be7f564-dbf2-4335-82c9-63c82004653d | Intransigent Locomotive Server             | public=2607:f298:5:101d:f816:3eff:fec6:6e9c, 208.113.135.120 |
| 040751d1-c4c5-47aa-8dec-1d69a468be1c | hnxhdkwskrvwvdwr                           | public=2607:f298:5:101d:f816:3eff:fe50:124, 208.113.130.11   |
+--------------------------------------+--------------------------------------------+--------------------------------------------------------------+

如果您更喜欢使用较新的客户端openstack客户端,则选项为:

 openstack server list -c Name -c Networks -f html

-f允许您选择输出格式csv,html,json,table,value,yaml。

答案 2 :(得分:0)

s_ref