Reading output of shell command in awk script

4

I have a text file with a list ip addresses and other information.

I am using an awk script to process this list and output various computations. I want to call dig -x from inside the awk script and use the returned value.

I have tried

hostname = system("dig +short -x" ip_address);

but what occurs is -

  1. the call to dig prints a line to the shell

  2. hostname remains null

bryan

Posted 2011-01-07T16:03:39.207

Reputation: 7 848

Answers

11

Figured it out, but please feel free to add better answers

cmd = "dig +short -x " ;
cmd ip_address | getline hostname;
close(cmd)

Then I can use the hostname anywhere in the script.

bryan

Posted 2011-01-07T16:03:39.207

Reputation: 7 848

1This is the correct way to do it. You will probably want to do close(cmd) afterwards. – Paused until further notice. – 2011-01-07T18:44:27.800

Yes thanks, found out about that after this posting, will close the question in 2 days(superuser requirement) – bryan – 2011-01-07T19:06:59.057