Skip to content
Advertisement

AWK to print subnet 0.69.100.in-abc.def

My requirement is to print a subnet 100.69.0.0/10 value to 0.69.100.in-abc.def. I tried as below

echo ['100.69.0.0/10'] | awk -F'.'  '{print $3"."$2"."$1".in-abc.def."}'

but i got output like this

0.69.[100.in-abc.def.

How to get output using awk as below

0.69.100.in-abc.def.

Advertisement

Answer

Good attempt and you were close, try following. With OP’s approach we need to put [ and ] as field separator too, so that we could catch the exact needed values by OP.

echo ['100.69.0.0/10'] | 
awk -F"[].[']" 'BEGIN{OFS="."} {print $4,$3,$2,"in-abc.def."}'

OR in case your echo is printing ['100.69.0.0/10'] then try following(where we have [,],.,' as field separators for awk):

echo "['100.69.0.0/10']" | 
awk -F"[].[']" 'BEGIN{OFS="."} {print $5,$4,$3,"in-abc.def."}'

Improvement in OP’s code(attempt): OP has added only . as field separator to make things easy we could add [ and ] too in field separators so that we could get the exact needed values.

NOTE: 1st solution is assuming that OP’s echo output is [100.69.0.0/10] which I believe could be wrong, OP’s echo output may have ' like ['100.69.0.0/10'](when using " to cover echo‘s output) in that case kindly use 2nd solution in above.

Advertisement