Ubuntu – How to remove brackets () around ip address from traceroute output

command linetext processing

I need to extract the 2nd IP address from a traceroute like so

traceroute -m2 8.8.8.8 |grep .net |awk '{print $3}'
(111.222.333.4444)

No matter what I try I can't seem to work out how to remove the surrounding brackets from the output.

Best Answer

You don't need grep in between, use AWK's pattern matching capability.

$ traceroute -m 2 8.8.8.8 | awk '/net/{gsub(/\(|\)/,"");print $3}'             
207.225.112.2
  • /net/ matches lines with the word net
  • gsub( /\(|\)/ , "" ) matches ( or ) and replaces them with empty string ( effectively deleting).
  • print $3 prints the 3rd item which is still the IP address , but without brackets