NAWK getline function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting NAWK getline function
# 1  
Old 11-26-2007
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

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

# 2  
Old 11-26-2007
I would think you could just do something like this:
Code:
cat /home/myscript/input | awk '{ /APPLE=/ { print $2 }'

# 3  
Old 11-26-2007
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.
# 4  
Old 11-26-2007
Quote:
I would think you could just do something like this:
Code:
cat /home/myscript/input | awk '{ /APPLE=/ { print $2 }'

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.
# 5  
Old 11-27-2007
Hi Perderabo,

Thanks alot!!
Your code works and thanks for the explanation too!!
# 6  
Old 11-28-2007
Quote:
Originally Posted by Raynon
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

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

you don't need the getline.
Code:
awk '/APPLE=/{print $2}' /home/myscript/input

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk getline function help

Hello All , I have to split a file as well as keep the header in all the splitted files. For this I am using the getline function of awk to keep the header however the catch is header is of 4 lines and I have to hold all the 4 lines by getline function(or is there any other option ???) into a... (5 Replies)
Discussion started by: Pratik4891
5 Replies

2. Shell Programming and Scripting

Nawk Problem - nawk out of space in tostring on

Hi.. i am running nawk scripts on solaris system to get records of file1 not in file2 and find duplicate records in a while with the following scripts -compare nawk 'NR==FNR{a++;next;} !a {print"line"FNR $0}' file1 file2duplicate - nawk '{a++}END{for(i in a){if(a-1)print i,a}}' file1in the middle... (12 Replies)
Discussion started by: Abhiraj Singh
12 Replies

3. Shell Programming and Scripting

nawk getline

I'm running the script below and get the output below against a file with lineA=aaa lineB=bbb lineC=ccc lineD=ddd I get output: lineC=ccc lineD=ddd I need the output to be: lineB=bbb lineC=ccc lineD=ddd cat filename | nawk '/lineA=aaa/ { getline; do { getline (3 Replies)
Discussion started by: toor13
3 Replies

4. Shell Programming and Scripting

Help using SYSTEM function in NAWK

I am scanning a file (line by line) for format errors. A line could have multiple errors. Each field in the line is evaluated for errors and sent, along w/ any error messages, to a temporary file. Finally, if any errors were detected, this temporary file is then appended to the errorFile. The... (4 Replies)
Discussion started by: aschera
4 Replies

5. Programming

getline()

I can not get 'getline()' to compile. I have tried. string curLine; //= compiler error char* curLine; //=compiler error char curLine; //=compiler error Every example I see uses a string as a getline(); parameter. It does not work for me on Fedora14 with gcc-c++. Thank you so much. This... (1 Reply)
Discussion started by: sepoto
1 Replies

6. Shell Programming and Scripting

Nesting - two nawk into one nawk

hi people; this is my two awk code: nawk '/cell+-/{r=(NF==8) ? $4FS$5FS$6 : NF==7 ? $4FS$5 : $4 ;c=split(r,rr);for (i=1;i<=c;i++){if(rr != "111111"){printf($3" %d ""\n",(i+3))}}printf("")}' /home/gc_sw/str.txt > /home/gc_sw/predwn.txt nawk -F'*' '{gsub(/ *$/,"")}$0=$1$($NF-2)'... (2 Replies)
Discussion started by: gc_sw
2 Replies

7. Shell Programming and Scripting

NAWK - looping with match() function

i'm trying to use the "before" output from the match() function as part of the results of each Regex match... but... My input data: (from an input file) i only show the first record in my file.. all other records are similar. mds_ar/bin/uedw92wp.ksh:cat $AI_SQL/wkly_inqry.sql... (2 Replies)
Discussion started by: danmauer
2 Replies

8. Shell Programming and Scripting

how to access values of awk/nawk variables outside the awk/nawk block?

i'm new to shell scripting and have a problem please help me in the script i have a nawk block which has a variable count nawk{ . . . count=count+1 print count } now i want to access the value of the count variable outside the awk block,like.. s=`expr count / m` (m is... (5 Replies)
Discussion started by: saniya
5 Replies

9. Shell Programming and Scripting

Nawk Time Function

Hi All, I am using solaris and nawk. Is there any time function in nawk which is simliar to the shell `date` function ? Can any experts show any examples? (4 Replies)
Discussion started by: Raynon
4 Replies

10. Shell Programming and Scripting

Nawk user-defined function

HELP!!!! I am in an on-line shell programming class and have a question. Here is the data: Mike Harrington:(510) 548-1278:250:100:175 Christian Dobbins:(408) 538-2358:155:90:201 Susan Dalsass:(206) 654-6279:250:60:50 (There are 12 contribuors total) This database contains names, phone... (1 Reply)
Discussion started by: NewbieGirl
1 Replies
Login or Register to Ask a Question