[grep] Is there a better way to do this?

Is there a way to retrieve the tun0's inet in a more reliable way? Currently, I am achieving this with the below code.

 sudo ifconfig | grep -E 'tun0 | inet ' | awk '{ print $2; }' |  awk '{ print $1; }' | awk 'NR==3{print $1}' 

The above code is not going to work any-longer if I add/remove/swap Ethernet cards around.

--- Post updated at 05:04 AM ---

I was able to achieve what I was looking for with the following:

sudo ifconfig | awk '/^tun0/{s=$1;getline;print $2}'

I hope this will help someone.

Hi, try this

ifconfig tun0 | awk '/inet / {print $2}'
2 Likes

Thank you! I like your way much better. It is even easier to understand. :b:

1 Like