Computing.Net > Forums > Unix > Unix n(awk) system() command

Unix n(awk) system() command

Reply to Message Icon

Original Message
Name: lalith
Date: March 8, 2005 at 16:02:06 Pacific
Subject: Unix n(awk) system() command
OS: solaris
CPU/Ram: unknown
Comment:

I read in lines from input text file (i.e. $0) where each line gets split into array (e.g. lines).
For each 2nd column (i.e. lines[2]), I wish to use the nawk system() command to search (using grep) this array column held value in lookup file (e.g. lookup.txt)?

In normal Unix this would be equivalent to `grep $lines lookup.txt`. The furthest I can construct is:
system("grep lines[2] lookup.txt | awk '{print $3}'") | getline captureresult

.....above system() command broken down is search lookup.txt for value held in array lines[2] and then get the 3rd column value of grep'd line found and capturing this 3rd column value into nawk variable captureresult !

Lalith


Report Offensive Message For Removal


Response Number 1
Name: Jim Boothe
Date: March 9, 2005 at 09:05:09 Pacific
Subject: Unix n(awk) system() command
Reply: (edit)

To pipe the output of a system command into getline, do not use the system function.  You just need to specify the command (which can be a series of constants and variables) piped into getline.  A simple example:

awk 'BEGIN{"ls|wc -l"|getline k; print k;exit}'

And do not include your variables in quotes.  One pain is in specifying the single quote (and maybe I am missing an easy way to do it).  Both solutions below work on my HP-UX, and the only difference is how I specify the single quotes.

awk '{
split($0,lines)
"grep " lines[2] " lookup.txt | awk '\''{print $3}'\''" | getline captureresult
print "captureresult=" captureresult
}' lalith.in

awk '{
q="\047"
split($0,lines)
"grep " lines[2] " lookup.txt | awk " q "{print $3}" q | getline captureresult
print "captureresult=" captureresult
}' lalith.in

But by grepping on the entire line, you may sometimes grep a line that you really do not want.  If the value you are wanting to match in lookup.txt is, let's say, in the first column (and you want the 3rd column from this matched line), it would be best to grep on column 1 only.  In the following example, I store the third columns from lookup.txt into an array, indexed by column 1.  Then for each input line, I check to see if the second field is in the stored array.

awk 'BEGIN{
while ((getline < "lookup.txt") > 0)
   lookup[$1]=$3}

{split($0,lines)
 word2=lines[2]
 if (word2 in lookup)
    print word2 "=" lookup[word2]
 else
    print word2 " not in lookup.txt"
}' lalith.in


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Unix n(awk) system() command

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge