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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Unix n(awk) system() command

Reply to Message Icon

Name: lalith
Date: March 8, 2005 at 16:02:06 Pacific
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



Sponsored Link
Ads by Google

Response Number 1
Name: Jim Boothe
Date: March 9, 2005 at 09:05:09 Pacific
Reply:

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


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Unix n(awk) system() command

Renaming files with awk www.computing.net/answers/unix/renaming-files-with-awk/8036.html

can I pass an awk variable to the s www.computing.net/answers/unix/can-i-pass-an-awk-variable-to-the-s/7251.html

to see unix file system in windows www.computing.net/answers/unix/to-see-unix-file-system-in-windows/3206.html