Passing argument to system call in awk script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing argument to system call in awk script
# 1  
Old 03-02-2010
Question 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.)


Code:
#!/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
# 2  
Old 03-02-2010
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:

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

# 3  
Old 03-02-2010
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:
Code:
{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?
# 4  
Old 03-02-2010
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.
# 5  
Old 03-02-2010
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
Code:
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:

Code:
./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. Smilie

Thanks!
# 6  
Old 03-02-2010
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:
Code:
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:
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:
Code:
awk -F, -f mike.awk $CSV


Last edited by vgersh99; 03-02-2010 at 08:01 PM.. Reason: spelling
# 7  
Old 03-02-2010
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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

2. Shell Programming and Scripting

passing argument in script?

hi, I want to implement some function to perform following task if ; then $TEXT = "Data_0" else $TEXT = $1 fi if ; then $Lines = 45 else $Lines = $2 fi Kindly suggest, thanks (11 Replies)
Discussion started by: nrjrasaxena
11 Replies

3. Shell Programming and Scripting

Passing --usage as argument to awk script

I have the awk script below and things go wrong when I do awk -v dsrmx=25 -f ./checkSRDry.awk --usage I basically want to override the usual --usage and --help that awk gives. How do people usually handle this situation when you also want to supply your own usage and help concerning the... (2 Replies)
Discussion started by: kristinu
2 Replies

4. Shell Programming and Scripting

Passing value as a command line argument in awk script.

I have one working awk command line. Which taking data from the “J1202523.TXT” file and generating the “brazil.dat” file. PFB code. awk '{ DUNS = substr($0,0,9);if ( substr($0,14,3) == "089" ) print DUNS }' J1202523.TXT > Brazil.dat But now I want to pass two parameter as a command line argument... (4 Replies)
Discussion started by: humaemo
4 Replies

5. Shell Programming and Scripting

Problem passing directory as argument with awk

I'm trying to figure out what's getting passed as the argument when I try to pass a directory as an argument, and I'm getting incredibly strange behavior. For example, from the command line I'm typing: nawk -f ./test.awk ~ test.awk contains the following: { directory = $NF print... (13 Replies)
Discussion started by: mrplainswalker
13 Replies

6. Shell Programming and Scripting

passing argument from Cshelll to awk command

Hi all I have got a file digits.data containing the following data 1 3 4 2 4 9 7 3 1 7 3 10 I am writing a script that will pass an argument from C-shell to nawk command. But it seems the values in the nawk comman does not get set. the program does not print no values out. Here is the... (1 Reply)
Discussion started by: ganiel24
1 Replies

7. UNIX for Advanced & Expert Users

how to use exceptfds argument in select system call

Hi, Can any one tell me how to use the fourth argument of select system call.I saw example "port forwarding" on the net,but it was too complex for me to understand.Can any one explain me about the usage of exceptfds argument of select system call with simple example. Thanks. (2 Replies)
Discussion started by: bvijaya
2 Replies

8. Shell Programming and Scripting

passing argument into awk

i'm trying to pass a numerical argument with function xyz to print specfic lines of filename, but my 'awk' syntax is incorrect. ie xyx 3 (prints the 3rd line, separated by ':' of filename) function xyz() { arg1=$1 cat filename | awk -F: -v x=$arg1 '{print $x}' } any ideas? (4 Replies)
Discussion started by: prkfriryce
4 Replies

9. UNIX for Dummies Questions & Answers

a system call for sed in a awk script

Hi, this is my test file : DELETE FROM TABLE WHERE ID_INTERNAL = :TABLE.ID-INTERNAL, ID-INTERNAL-CRAZY ID-INTERNAL-OPEN ID-INTERNAL /ID-INTERNAL/ I want all occurences of ID-INTERNAL replaced with a one, if ID-INTERNAL has and dash afer it , dont replace it example:... (6 Replies)
Discussion started by: seaten
6 Replies

10. UNIX for Dummies Questions & Answers

Passing argument to awk script

I am writing a shell script. Now i need to read in a string and send it to an awk file to compare and search for compatible record. I wrote it like tat: read serial | awk -f generate.awk data.dat p/s: the data file got 6 field. According to an expert, we can write it like tat: read... (1 Reply)
Discussion started by: AkumaTay
1 Replies
Login or Register to Ask a Question