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 |grep .net |awk '{print $3}'

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 | awk '/net/{gsub(/\(|\)/,"");print $3}'    
  • /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