Passing argument to system call in awk script

So, I have this script. It reads a CSV file that has a mixture of object names with IP addresses (parsing out that part I have working), and object names which have a DNS name. I want to be able to run a "dig +short" based off of the name given to me in the line of the awk script, and then deal with that as it comes. (Ideally, for things where dig returns more than one IP, I'll be able to say object_1 xxx.xxx.xxx.xxx, object_2 xxx.xxx.xxx.yyy, and so forth.)

#!/bin/bash
cat  $CSV | egrep -v "Network Entity" | awk -F, '
        {for (i=1;i<=NF;i++)
                {if (some conditions) )
                        { Bunch of commands which are working fine}
                          
                else
                      {if (($i == "Network address") && ($(i+1) !~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/))
                          {
                               #print the object name
                               {printf $1 ","};
                               #Use the object data to run dig
                               {print system("dig +short" $(i+1);}

                          }

                      }
                
                }
        }' > $HOSTS_AND_SUBNETS

I'm getting a syntax error. How do I pass an argument to a system call?

Thanks in advance,
Mike

I think you need a space after +short.
A good practices approach might be to use sprintf with a format string so you know what you are getting:

{print system( sprintf("dig +short %s", $(i+1)) ) }

Ah. The missing %s is what did it. Thank you.

So, armed with that information, I'm now getting a syntax error when I do this:

{if (($i == "Network address") && ($(i+1) !~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/))
                          {
                               {printf $1};
                               {system("dig +short %s A | awk '{for (k=1;k<=NF;k++) {if ($k ~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/) {printf $k ","}}'" $(i+1));}

                          }

                      }

Does it not like embedded awks in a system call?
Is there a better way to do this?

system invokes a different shell. usually it is /bin/sh - whatever that turns out to be.
That may be the problem or the fact that command is not right.

Simple is better. Build the string first, then call system( one_string ).

Or extract the complex string and run it on the command line til it works. Then stick it back into the script between the backtics.

How can I build the string before I call the system on the string?
The basic flow is this:

  1. Read the next line in the file.
  2. Cycle through each field in the file until it sees "Network address"
  3. Knowing that the next field in the file after "Network address" is the DNS name of the
    host, pass said DNS name to dig.
  4. Put all of the IP addresses returned from dig, comma separated, on the line after the host name.

The command I'm passing works on the command line.

If I paste

dig +short +ignore +nonssearch +noauthority +noadditional -4 www.microsoft.com A  | awk '{for (k=1;k<=NF;k++) {if ($k ~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/) {printf $k ","}}}'

on the command line, it works.

When I encase that in a print system([that string]) line, I get the following:

./convert: line 59: syntax error near unexpected token `('
./convert: line 59: `                               {print system("dig +short +ignore +nonssearch +noauthority +noadditional -4 %s A  | awk '{for (k=1;k<=NF;k++) {if ($k ~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/) {printf $k ","}}}'" $(i+1));}'

I tried escaping the apostrophes (backticks produced even more errors) and that didn't help either.

Take pity on me. I'm a router / switch / firewall guy who is having to write scripts to translate from Symantec SGS to Cisco. I speak Cisco. I'm brand new to scripting. :frowning:

Thanks!

the issue is with the embedded single-quotes in the command you're executing with 'system'.

create a file mike.awk with the content of your awk 'stuff' and call it like so:

cat  $CSV | egrep -v "Network Entity" | awk -F, -f mike.awk

While you're at it, you can get rid of 'cat|egrep' all together and do it all natively within the awk code:

!/Network Entity/ {for (i=1;i<=NF;i++)
                {if (some conditions) )
                        { Bunch of commands which are working fine}
.....
.....

In this case your calling sequence will be:

awk -F, -f mike.awk $CSV

Breaking it out into a separate file worked, that's perfect.

You guys rock. Hopefully, I'll not have to bother you further.

THANK YOU!