Or don’t, I’m not your father.
The situation Sometimes, you want to get a very specific part of a command output. The classic problem is to get the IP address of an interface. Even today, the results to these queries on your favorite search engine will most likely involve grep, sed, awk and the like.
But what if I told you there’s a better way?
Using JSON for structuring data Some tools offer outputting their data in JSON format. That data can be processed with the venerable jq utility. The control you have over the processing is higher and the chance that the data format of the output changes is lower. Let’s look at what that means in practice.
Isn’t it incredible, that we have to read blogposts to learn how to extract an IP addr of a network interface in the shell … in json … in 2025 ??!
Some problems will never really get solved.
Thanks for the write up !
You’re welcome! And actually, even this approach can yield surprising results… As in have you heard of deprecated IPv6 addresses before? Well I hadn’t until I realized my interface now had one (it actually didn’t anymore when I wrote the post, I used the jq command on old output, not in a pipe). Which made my DynDNS script stop working because there was now a line break in the request URL that curl rightfully failed on.
Edit: also despite what the title of the post says, in not an authoritative expert on the matter and you can use whatever works for you. I see these posts more as a basis for discussions like here than definitive guides to do something the correct way.
Definetly not 0.o
It’s really hard to actually believe that a problem like this hasn’t got a 1-word-command + flag solution yet. I mean you could ecxpect something like
ip -6 -i eno0
or so…
And yes, totally agree on the edit part! It’s always nice to at least no about all the options that exist and smb found out hustling the same struggle like me :D