Computing.Net > Forums > Unix > Pause during gawk getline loop

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.

Pause during gawk getline loop

Reply to Message Icon

Name: eugene tan
Date: January 31, 2003 at 11:28:21 Pacific
OS: win2k/cygwin
CPU/Ram: P3/512Mb
Comment:

Hi there,

I trying to do some basic stuff with shell scripting and awk in cygwin. My main goal is actually to parse a textfile and check the source depot for changes and provide a report.

But baby steps first. From the various posts I read. I've used the following nifty function to print out the contents of a textfile.

#!/bin/sh
gawk '{

while (getline 0)
{
print $0
}
exit
}'

When I run this snippet I notice before it starts looping and displaying my textfile it will sit and wait for a CR.

What am I missing?. How come getline isn't just going straight into processing the textfile?

cheers,
ET




Sponsored Link
Ads by Google

Response Number 1
Name: James Boothe
Date: February 1, 2003 at 08:14:09 Pacific
Reply:

awk does the getlines (into $0 buffer) for you automatically. A basic awk solution will not use an explicit getline, but getline comes in very handy in many cases.

So we let awk read the lines. We specify line selection criteria and what to do when we find those lines. Let's say we have a file named myfile containging 6 lines. To print all lines (like the cat command):

awk '{print}' myfile
line A
So this guy walks
line B
into a bar with a
line C
duck under his arm ...

Now find lines that start with "line" and print them:

awk '/^line/ {print}' myfile
line A
line B
line C

And if what you want to do for each qualifying line (an action statement in braces) is simply "print", you can omit that because that is the default action:

awk '/^line/' myfile
line A
line B
line C

But if you want to do something other than print the entire line, such as print the second word, then:

awk '/^line/ {print $2}' myfile
A
B
C

But say we want to print the lines that follow each line that begins with "line". In this case, since I know my data, I could cheat and print only those lines that have more than two fields ('NF>2'), but the sure way to get them is: each time we find a line begninning with "line", do an explicit read of the next line and print it:

awk '/^line/ {getline;print}' myfile
So this guy walks
into a bar with a
duck under his arm ...

Consider this program:

awk '{getline;print}' myfile

awk presents the first line, and we are saying "get the next line and print". Then awk automatically reads the next (3rd) line, and we say "get the next line and print". Therefore, the above program will print the 2nd, 4th, 6th, etc lines.

getline can also read lines from a file other than the main file being processed by awk, and in this case, it would be appropriate to have that in a while-getline loop.


0

Response Number 2
Name: eugene tan
Date: February 6, 2003 at 11:19:46 Pacific
Reply:

Thanks for the informative response.
You've help me simplify and clarify things

CHeers,
ET


0

Sponsored Link
Ads by Google
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: Pause during gawk getline loop

Script: Read file to set variables www.computing.net/answers/unix/script-read-file-to-set-variables/3849.html

Initializing files to empty in korn www.computing.net/answers/unix/initializing-files-to-empty-in-korn/8185.html

korn or unix www.computing.net/answers/unix/korn-or-unix/6382.html