Search code examples
linuxbashcut

Parse only the IP address from resolvectl


I want to cut only the IP address. I tried cut -d':' -f1 but that takes other words that I don't want.

resolvectl query remote.geo.com

but I have :

remote.geo.com: X.X.X.X           -- link: enp0s31f6

I want only

X.X.X.X

I can take the first line but there are other words.


Solution

  • Like most modern commands, resolvectl has a flag to turn off decorations, which makes it somewhat easier to parse its output.

    resolvectl query --legend=false remote.geo.com | awk '{print $2}'
    

    If you are not hellbent on using resolvectl, you can get machine-readable output from dig or (some versions of) host:

    dig +short remote.geo.com