Black_Zero
Black_Zero

Reputation: 475

How to extract multiple IP addresses in awk in a single line

I have an output from an openstack instance as follows:

[atlasadm@atlas (mrf-tenant)]$ nova list --name mrf
+--------------------------------------+--------------+--------+------------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| ID                                   | Name         | Status | Task State | Power State | Networks                                                                                                                                                                              |
+--------------------------------------+--------------+--------+------------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 502ca64d-1661-49d9-8468-9ee4cc574f39 | mrf          | ACTIVE | -          | Running     | mrf_signaling=10.0.0.27; mrf_trusted=fd00:1::1b, 10.1.0.27; mrf_internal=192.168.0.5; mrf_mgmt=131.160.102.4; mrf_rsrv=10.3.0.27; mrf_untrusted=fd00:2::1b, 10.2.0.27 |
+--------------------------------------+--------------+--------+------------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

I now need to extract the three different ip addresses and set the three variables as an example below:

mrf_management=131.160.102.4
mrf_trusted=10.1.0.27
mrf_untrusted=10.2.0.27

I am trying this but its giving me the ip addresses with semicolons ";" as follows:

[atlasadm@atlas (mrf-tenant)]$ nova list --name mrf | awk '/trusted/ {print $14 $16}'
10.1.0.27;10.2.0.27;

How can I extract these addresses without the semicolon and set the variables in one line? Is that even possible in one line?

Upvotes: 0

Views: 406

Answers (3)

Ed Morton
Ed Morton

Reputation: 203502

With GNU awk for multi-char RS and RT:

$ awk -v RS='mrf_[^;|]+' -F'[= ,]+' 'RT{$0=RT" "; print $1"="$(NF-1)}' file
mrf_signaling=10.0.0.27
mrf_trusted=10.1.0.27
mrf_internal=192.168.0.5
mrf_mgmt=131.160.102.4
mrf_rsrv=10.3.0.27
mrf_untrusted=10.2.0.27

If you want to create variables with those names use declare $(awk...) when calling from shell.

Upvotes: 1

Mustafa DOGRU
Mustafa DOGRU

Reputation: 4112

maybe this could help you.

nova list --name mrf  | awk -F';' '/trusted/ {printf "%s\n%s\n%s\n", $4, $2, $6}' |sed -e 's/\=.*,/=/;s/|//'

or

nova list --name mrf  | awk -F';' '/trusted/ {printf "%s\n%s\n%s\n", $4, $2, $6}' 

Upvotes: 0

EvansWinner
EvansWinner

Reputation: 158

It seems like your data has fields within fields. What about piping to another call to awk? Maybe there is a more elegant way of doing it, but something like:

nova list --name mrf  | awk 'BEGIN {FS="|"} ; {print $7}' | awk 'BEGIN {FS=";"} ; /trusted/ {print $1; print $2; print $3; print $4; print $5; print $6}'

Upvotes: 0

Related Questions