nawk getline


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting nawk getline
# 1  
Old 02-04-2011
Computer nawk getline

I'm running the script below and get the output below against a file with
Code:
lineA=aaa
lineB=bbb
lineC=ccc
lineD=ddd

I get output:
Code:
lineC=ccc
lineD=ddd

I need the output to be:
Code:
lineB=bbb
lineC=ccc
lineD=ddd

Code:
cat filename | nawk '/lineA=aaa/ {
getline;
do {
getline
print
 } while ( $1 != "lineD=ddd" )
}'

I can go further up the file for my starting value and get what I need but I just need to figure out why it's skipping the first line of what it finds when it prints the output.

Thanks in advance.

Last edited by Scott; 02-04-2011 at 12:13 PM.. Reason: Code tags, please...
# 2  
Old 02-04-2011
You've used two getline's. That's why it doesn't print the first two lines.

Code:
awk '/lineA=aaa/ {
do {
getline
print
 } while ( $1 != "lineD=ddd" )
}' file1

After the second getline, $0 will be the third line (LineC=ccc).
# 3  
Old 02-04-2011
can this code help you?
Code:
 awk '/lineA=aaa/{p=-1}{p++}p' file

# 4  
Old 02-04-2011
Your spec can be described as "Keep printing lines from the one following the first matched to the last matched". You match the first line that you want, so you get a new line (getline), print it and then ask: Is this the line I want to match last? If yes then quit; otherwise, keep getting lines. So, as scottn points out, the first getline, the one that is out of the loop, should not be there because that is not what you want.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Programming

getline() and fclose()

Hi All, When I do man getline on my Linux system (in BASH), I see an example C code where a file is being read using getline() function. Now, my question is why there is no fclose() to close the open file pointer? People who don't have a Linux system, this is the code that I am referring... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

3. Shell Programming and Scripting

getline from another file

Here is I want to do: find lines in file filteredtrace.txt which is not continued as a multiply of 4 and strip them from file original_trace_framno. problem is awk used the ' symbol so pipe of getline has to use the " symbol while agument of sed has to use the " symbol, it doesn't... (8 Replies)
Discussion started by: lzq420241
8 Replies

4. 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

5. 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

6. 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

7. Shell Programming and Scripting

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 ... (5 Replies)
Discussion started by: Raynon
5 Replies

8. Shell Programming and Scripting

awk getline help maybe?

hello collegues, I am attempting to use awk to search file1 (serverlist.csv) from each row with file2 (supported.txt). If the is no entry exists in serverlist then output to a file called notsupp.out if there is an entry output to supp.out I can do this with basic shell scripting however... (0 Replies)
Discussion started by: chlawren
0 Replies

9. UNIX for Dummies Questions & Answers

utility of getline here?

hi , I got a script that I don't understand. awk '{ command="echo "$1 command | getline echome close(command) print $0 "\t" echome }' < user.list It reads the file user.list user.list London John Bridge Peter and sends the stream to the awk command. I don't understand what is... (3 Replies)
Discussion started by: remid1985
3 Replies

10. Shell Programming and Scripting

getline with a unique

I have an inputfile that I trying to awk with a getline(Solaris v8 ksh). However I need to have the results be a unique. Is there a comparable command within awk to return a unique result? Or can you break out of the awk to add a uniq. I am looking for something more complex then a pipe... (6 Replies)
Discussion started by: gozer13
6 Replies
Login or Register to Ask a Question