Reputation: 994
I have a command that outputs something like this:
192.168.1.1 182
192.168.1.10 300
192.168.4.50 64
I want to pipe this through sed or some other linux command-line tool and replace the IP addresses with their hostname from the 'host ' command, like this:
web.hostname.com 182
db.hostname.com 300
search.hostname.com 64
How would I go about doing this?
Upvotes: 1
Views: 2331
Reputation: 40407
I can't figure out how to get sed to use the evaluation of a command (in backticks) in the replace string and pass it one of the matched subexpressions as an argument, but maybe someone else can.
If you are in bash, you could pipe it into this:
while read ip whatever ; do hostname=`host $ip | grep pointer` ; \
if [ -n "$hostname" ] ; then echo `host $ip` $whatever | \
sed 's/\(^.* pointer \)\(.*\)\./\2/g' ; else echo $ip $whatever ; fi ; done
(This tries to pull the hostname out of the response, and leave the ip address numeric if the result of the lookup isn't a hostname)
Upvotes: 0
Reputation: 75774
Looks a bit tricky, but should do what you need:
your_command | while read HOST NUM; do host $HOST | tr "\n" " "; echo $NUM; done
Upvotes: 1