首页 > 解决方案 > 使用 shell 脚本从具有指定条件的行中获取特定的列值

问题描述

我有一个命令的示例输出

+--------------------------------------+------------------+---------------------+-------------------------------------+ 
| id                                   | fixed_ip_address | floating_ip_address | port_id                             |
+--------------------------------------+------------------+---------------------+-------------------------------------+
| 04584e8a-c210-430b-8028-79dbf741797c |                  | 99.99.99.91        |                                      |
| 12d2257c-c02b-4295-b910-2069f583bee5 | 20.0.0.92        | 99.99.99.92        | 37ebfa4c-c0f9-459a-a63b-fb2e84ab7f92 |
| 98c5a929-e125-411d-8a18-89877d3c932b |                  | 99.99.99.93        |                                      |
| f55e54fb-e50a-4800-9a6e-1d75004a2541 | 20.0.0.94        | 99.99.99.94        | fe996e76-ffdb-4687-91a0-9b4df2631b4e |
+--------------------------------------+------------------+---------------------+-------------------------------------+

现在我想获取“port_id”和“fixed_ip_address”字段为空白/空的所有“浮动_ip_address 上面的示例99.99.99.91和99.99.99.93中)

我如何使用 shell 脚本来做到这一点?

标签: bashshellubuntufor-loopopenstack

解决方案


您可以使用sed

fl_ips=($(sed -nE 's/\|.*\|.*\|(.*)\|\s*\|/\1/p' inputfile))

inputfile是问题中提供的表格。该数组fl_ips包含以下输出sed

>echo ${#fl_ips[@]}
2                    # Array has two elements
>echo ${fl_ips[0]}
99.99.99.91
>echo ${fl_ips[1]}                                                         
99.99.99.93                                          

推荐阅读