Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

In a script I want to be able to write an IP address to somewhere easily, so I thought using dig (or a similar command) with back-ticks.

However the simplest output I've been able to come up to wrt dig parameters is

> dig -t A +noall +answer www.google.com
www.google.com.     300 IN  A   173.194.66.106
www.google.com.     300 IN  A   173.194.66.104

Any way (extra arg, different tool instead of dig?) to get rid of the junk apart from the IP address?? (And please don't tell me to use sed.)

Thanks

share|improve this question

2 Answers 2

up vote 7 down vote accepted

On the CentOS box I have to hand

dig +short   www.google.com
74.125.132.147
74.125.132.99
74.125.132.103
74.125.132.104
74.125.132.105
74.125.132.106

If you only want one address then

dig +short   www.google.com | head -1

or

dig +short   www.google.com | tail -1
share|improve this answer

sed is a viable option and outright rejecting it is misguided at best.

Anyway, try

dig -t A +noall +answer www.google.com | cut -f 6 | tail -1
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.