All the postprocessing requested can be done internal to awk
. Expanding a one-liner provided in a comment by @123 for better readability, this can look like the following:
ip -o -f inet addr show | \
awk -v i="$INTERNAL" '
$0 ~ i && /scope global/ {
sub(/\//, "_", $4);
print $4;
}'
Breaking down how this works:
awk -v i="$INTERNAL"
defines an awk variable based on a shell variable. (As an aside, all-caps shell variable names are bad form; per POSIX convention, lowercase names are reserved for application use, whereas all-caps names can have meaning to the OS and surrounding tools).
$0 ~ i
filters for the entire line ($0
) matching the awk variable i
.
/scope global/
by default is applied as a regex against $0
as well (it's equivalent to $0 ~ /scope global/
).
sub(/\//, "_", $4)
substitutes /
s with _
s in the fourth field.
print $4
prints that field.