【问题标题】:Fetch particular column value from rows with specified condition using shell script使用 shell 脚本从具有指定条件的行中获取特定的列值
【发布时间】:2018-10-12 05:21:21
【问题描述】:

我有一个命令的示例输出

+--------------------------------------+------------------+---------------------+-------------------------------------+ 
| 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" 字段为 空白的所有 "floating _ip_address" /empty(在上述示例 99.99.99.91 和 99.99.99.93 中)

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

【问题讨论】:

  • 你都试过什么?

标签: bash shell ubuntu for-loop openstack


【解决方案1】:

你可以使用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                                          

【讨论】:

    猜你喜欢
    • 2019-10-15
    • 2020-06-18
    • 2023-03-08
    • 1970-01-01
    • 2021-01-16
    • 2022-11-14
    • 1970-01-01
    • 2014-04-05
    相关资源
    最近更新 更多