Reputation: 925
I use the following command sipcalc
to display information about an IP:
sipcalc 192.16.12.1/16 | grep -E 'Network address|Network mask \(bits\)'
The output is:
Network address - 192.16.0.0
Network mask (bits) - 16
Is there a way to combine the above output (only the right part), so the output would be:
192.16.0.0/16
I have my own way to do this by separating grep call and then concatenate the result, but I don't think it is a good solution. Can grep or any other commands that can be used to pipe the output like awk
in order to obtain the output above?
Upvotes: 0
Views: 169
Reputation: 85895
grep
is not really an ideal tool for doing operations beyond just searching for your expected text. Use awk
alone!
awk '/Network address/{ ip = $NF } /Network mask \(bits\)/{ print ip "/" $NF}'
Awk processes records in /pattern/ { action }
syntax. So when the first pattern in matched, extract the last field delimited by space $NF
i.e. a special variable Awk uses to store the value of last column when delimited by space ( See 7.5.1 Built-in Variables That Control awk)
When the second pattern is matched in a similar way, join that last field with the value stored in ip
variable. The +
just concatenates the individual strings to produce the desired result.
Upvotes: 2