MacOS – Mac: Regular expression to parse scutil command output

bashmacmacos

I've below output from scutil --nc show <service id>

Connected
Extended Status <dictionary> {
  ConnectionStatistics : <dictionary> {
    ConnectCount : 6
    ConnectedCount : 3
    DisconnectedCount : 5
    MaxConnectTime : 1874
  }
  IPv4 : <dictionary> {
    Addresses : <array> {
      0 : 10.20.15.181
    }
    ExcludedRoutes : <array> {
      0 : <dictionary> {
        DestinationAddress : 115.112.149.120
        InterfaceName : en0
        SubnetMask : 255.255.255.255
      }
      1 : <dictionary> {
        DestinationAddress : 115.112.149.120
        InterfaceName : en0
        SubnetMask : 255.255.255.255
      }
      2 : <dictionary> {
        DestinationAddress : 115.112.149.120
        InterfaceName : en0
        SubnetMask : 255.255.255.255
      }
      3 : <dictionary> {
        DestinationAddress : 115.112.149.120
        InterfaceName : en0
        SubnetMask : 255.255.255.255
      }
      4 : <dictionary> {
        DestinationAddress : 115.112.149.120
        InterfaceName : en0
        SubnetMask : 255.255.255.255

I wanted to take out ip addresses from Addresses : <array> from below

IPv4 : <dictionary> {
    Addresses : <array> {
      0 : 10.20.15.181
      1 : 10.20.15.182
    }

How to write regular expression for it?

Best Answer

You could try something like this:

scutil --nc show <service id> | sed -n '/IPv4/,/ExcludedRoutes/p' | sed '/ExcludedRoutes/,/{/d'
  • sed -n '/IPv4/,/ExcludedRoutes/p' will show text between IPv4 and ExcludedRoutes, both included.
  • sed '/ExcludedRoutes/,/{/d' will remove the ExcludedRoutes line.

The result will be like this:

$ cat file <replace-with-your-command> | sed -n '/IPv4/,/ExcludedRoutes/p' | sed '/ExcludedRoutes/,/{/d'
  IPv4 : <dictionary> {
    Addresses : <array> {
      0 : 10.20.15.181
    }