bash - 使用 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 脚本来做到这一点?
解决方案
您可以使用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
推荐阅读
- c++ - Pytorch C++ API:CMake 问题
- java - JDBI -> 将 MySql DATETIME 读入 java.sql.Date 或类似的
- dart - Dart 是否具有尾调用优化 (TCO) 功能?
- python - 如何使用自定义内核导出 SVM 模型?
- python - 有没有办法让 Pyinstaller GUI 引用与其他 GUI 的共享文件夹,以减少磁盘上的冗余库文件
- javascript - Mongoose .lean() 方法的相等性测试失败
- c# - 具有数据库优先多对多关系问题的实体框架 6
- java - 如何使用 JComboBox 更改背景颜色
- matlab - 关于 Matlab 中 FFT 的零填充
- python - Pandas Pivot Table 每个组有两行