通过shell脚本从输出中提取ID

时间:2019-03-20 19:38:12

标签: shell grep

当我执行echo $ myvariable时,我在脚本外壳中有一个变量。这是输出:

git commit -m "<your commit message>"

然后,我试图提取子网ID {"networks":[{"status":"ACTIVE","router:external":true,"availability_zone_hints":[],"availability_zones":["nova"], "ipv4_address_scope":null,"description":"", "port_security_enabled":true,**"subnets":["56efe610-32af-4f03-a73d-14bcbf1c9ae1","18ca945c-8868-4549-b725-e11f04612663"],** "updated_at":"2018-02-26T04:55:25Z", *"tenant_id":"187d635aec4c43fe8e8918afb3a5c82e",* "created_at":"2018-02-26T04:55:12Z", "tags":[],"ipv6_address_scope":null, "mtu":1500,"is_default":true,"revision_number":7, "admin_state_up":true,"shared":false, "project_id":"187d635aec4c43fe8e8918afb3a5c82e", "id":"7697d4c6-5b4c-4ea9-a1d6-af7d7f716f2b","name":"public" 和tenant_id((56efe610-32af-4f03-a73d-14bcbf1c9ae1 and 18ca945c-8868-4549-b725-e11f04612663))。

我尝试了这个shell脚本for tenant_id:

187d635aec4c43fe8e8918afb3a5c82e

echo $ tenantid我得到相同的结果。

对于子网,我尝试了相同的方法,但结果相同。

tenantid=$("$myvariable" | grep "tenant_id" | awk '{printf $2}')

先谢谢大家。

1 个答案:

答案 0 :(得分:0)

这似乎可行:

subnets=`echo $myvariable | egrep -o '"subnets":\[[-_A-Za-z0-9, "]*\]' | egrep -o '\[[-_A-Za-z0-9, "]*\]'`
tenant_id=`echo $myvariable | egrep -o '"tenant_id":"[-_A-Za-z0-9 ]*",'  | egrep -o ':"[-_A-Za-z0-9 ]*"' | egrep -o '"[-_A-Za-z0-9 ]*"'`

我不确定您想要哪种格式。