匹配字符串,其中初始部分变量和固定结束部分

时间:2016-02-12 10:12:08

标签: regex shell grep

以下是nova命令输出中的实例名称列表。

nova list    

 +--------------------------------------+-----------------------------------------+--------+------------+-------------+------------------------------------------+
| ID                                   | Name                                    | Status | Task State | Power State | Networks                                 |
+--------------------------------------+-----------------------------------------+--------+------------+-------------+------------------------------------------+
| 6cdc00a7-cfe3-4bfe-bbb1-7980ac1c04c0 | haproxy-instance-vms22updateconfar                         | ACTIVE | -          | Running     | Orch-Mgmt=10.32.1.40                     |
| d0528617-39cd-4098-b34c-0977f5a18414 | gunicon-instance-vms22updateconfar                    | ACTIVE | -          | Running     | vms2.1-net=192.168.0.248 |
| e89dd43d-8021-47c6-9f55-39d8bce3c11b | nsoshim-instance-vms22updateconfar                         | ACTIVE | -          | Running     | App-Mgmt=10.20.0.126    |
| b7ea9059-834c-4196-8706-54cfaab3d177 | haproxy-instance-vms22update                        | ACTIVE | -          | Running     | App-Mgmt=10.20.0.89     |
| 2d4d22e5-b844-413f-8d36-f8b3eb3dea32 | gunicon-instance-vms22update                       | ACTIVE | -          | Running     | App-Mgmt=10.20.0.46     |
| 41c4fdc0-3058-4e39-8207-2c02a611ee22 | nsoshim-instance-vms22update             | ACTIVE | -          | Running     | App-Mgmt=10.20.0.217                     |                 
                 |



SUBDOMAIN=vms22update

nova list | grep "\-instance-$SUBDOMAIN"

gunicon-instance-vms22updateconfar
haproxy-instance-vms22updateconfar
nsoshim-instance-vms22updateconfar

gunicon-instance-vms22update

haproxy-instance-vms22update
nsoshim-instance-vms22update

我希望看到实例仅以vms22update结束

我试过nova list | grep" -instance - ^ $ SUBDOMAIN $" 它没有列出任何东西。

1 个答案:

答案 0 :(得分:0)

@Chris_vr:感谢提示将我的评论作为答案发布:

你可以试试这个:

TABLE
  1. 执行nova list | awk -F"|" '{print $3}' | sed 's/ *$//' | grep -E "vms22update\$"
  2. 获取输出
  3. nova list
  4. 拆分
  5. 删除空格
  6. grep表示以|
  7. 结尾的行