NAWK getline function

Hi,

I am using a Solaris OS here. My intention is to print the 2nd field if the first field matches "APPLE=". I am using the "getline" function here (shown below), but it doesn;t work. Can any experts give me some advice?

Input:
ORANGE= 10
APPLE= 20
MANGO= 30
GRAPES= 40

Output:
20

nawk '{
if (($1 < getline "/home/myscript/input") == "APPLE=") {number = $2; print number}}'

I would think you could just do something like this:

cat /home/myscript/input | awk '{ /APPLE=/ { print $2 }'

Hi Smiling Dragon,

Thanks for your suggestion . I have thought about that too. However, this awk statement of mine is being embedded in a larger awk program. It would be cleaner and more convenient to use " getline " to get the input file as there are more than 10 input files. In this example i am only showing one input file.

Or just:
awk '{ /APPLE=/ { print $2 }' /home/myscript/input

We don't need that cat process. Doing this with getline would be crazy, but this should be close:
awk 'BEGIN { while (getline < "/home/myscript/input") { if ($1 == "APPLE=") print $2 } }'

  1. You need the BEGIN to run the awk script before awk tries to use the default stdin.
  2. You need to to explicitly loop reading via getline
  3. getline does not return the data it reads, it returns a success code.

Hi Perderabo,

Thanks alot!!
Your code works and thanks for the explanation too!!

you don't need the getline.

awk '/APPLE=/{print $2}' /home/myscript/input