For Programmers: Free Programming Magazines  


Home > Archive > AWK > November 2004 > getline startup?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author getline startup?
Tom

2004-11-16, 6:50 pm

Oh wise ones,

The following simple piece of code will not run untill I hit the enter key.
What am I overlooking?
Sometimes I go brain dead on the simple things.
I'm using gawk 3.1.1 on RH9.

executed as:

[bin]# gawk -f prog.awk (ENTER)
Here,on the next line, I have to hit (ENTER) again.

Please help, Tom

BEGIN {
FS = " "
i = 1
}

{
while ((getline < "data") == 1) {
the_list[i] = $1
print i, the_list[i]
i += 1
}
close("data")
print i
exit
}

"data" is a text file containing 3 lines:
test1
test2
test3


Kenny McCormack

2004-11-16, 6:50 pm

In article <9a6dnR5jUvHUAxLcRVn-sA@adelphia.com>, Tom <tombro@usa.net> wrote:
>The following simple piece of code will not run untill I hit the enter key.
>What am I overlooking?
>Sometimes I go brain dead on the simple things.
>I'm using gawk 3.1.1 on RH9.
>
>executed as:
>
>[bin]# gawk -f prog.awk (ENTER)
>Here,on the next line, I have to hit (ENTER) again.
>
>Please help, Tom
>
>BEGIN {
> FS = " "
> i = 1


3 lines deleted. Now, it should be OK.

>while ((getline < "data") == 1) {
> the_list[i] = $1
> print i, the_list[i]
> i += 1
> }
>close("data")
>print i
>exit # You could delete this, too.
>}


E. Rosten

2004-11-16, 6:50 pm

Tom wrote:
> Oh wise ones,
>
> The following simple piece of code will not run untill I hit the enter key.
> What am I overlooking?
> Sometimes I go brain dead on the simple things.
> I'm using gawk 3.1.1 on RH9.
>
> executed as:
>
> [bin]# gawk -f prog.awk (ENTER)
> Here,on the next line, I have to hit (ENTER) again.
>
> Please help, Tom
>
> BEGIN {
> FS = " "
> i = 1
> }
>
> {
> while ((getline < "data") == 1) {
> the_list[i] = $1
> print i, the_list[i]
> i += 1
> }
> close("data")
> print i
> exit
> }
>
> "data" is a text file containing 3 lines:
> test1
> test2
> test3



Pattern / action blocks take the following form:

pattern{action}

The test is repeates once for every line (well, record) of the file. In
this case, the file is standard input, so you need to provide some by
pressing enter.

You should try to rewrite the program so you type:

gawk -f prog.awk data

-Ed


--
(You can't go wrong with psycho-rats.) (er258)(@)(eng.cam)(.ac.uk)

/d{def}def/f{/Times findfont s scalefont setfont}d/s{10}d/r{roll}d f 5/m
{moveto}d -1 r 230 350 m 0 1 179{1 index show 88 rotate 4 mul 0 rmoveto}
for /s 15 d f pop 240 420 m 0 1 3 { 4 2 1 r sub -1 r show } for showpage

Bob Harris

2004-11-16, 6:50 pm

In article <9a6dnR5jUvHUAxLcRVn-sA@adelphia.com>,
"Tom" <tombro@usa.net> wrote:

> Oh wise ones,
>
> The following simple piece of code will not run untill I hit the enter key.
> What am I overlooking?
> Sometimes I go brain dead on the simple things.
> I'm using gawk 3.1.1 on RH9.
>
> executed as:
>
> [bin]# gawk -f prog.awk (ENTER)
> Here,on the next line, I have to hit (ENTER) again.
>
> Please help, Tom
>
> BEGIN {
> FS = " "
> i = 1
> }
>
> {
> while ((getline < "data") == 1) {
> the_list[i] = $1
> print i, the_list[i]
> i += 1
> }
> close("data")
> print i
> exit
> }
>
> "data" is a text file containing 3 lines:
> test1
> test2
> test3


Since this script does not read a file via standard input or via file
(or files) specified on the command line, then you should put the entire
script inside of the BEGIN{...} pattern{action} section.

Or instead of using getline, rewrite to use awk's natural abilities

awk '
BEGIN{ FS = " " }
{
the_list[++j] = $1
print j, the_list[j]
}
END { print j }
' data

Of course I'm assuming you really have more to this script, otherwise
there is not real use for 'the_list' as coded, and FS already separates
based on white space, so unless you really wanted just blanks and not
tabs, you do not need to specify FS in the BEGIN block

awk '{ print NR, $1 } END{ print NR }' data

Should do the same as your script. Again, this assumes that your script
is only doing what is shown and is not just an example of a much larger
and more complex script.

Bob Harris
Tom

2004-11-16, 6:50 pm


"Bob Harris" <harris@zk3.dec.com> wrote in message
news:harris-053ECD.13170408112004@cacnews.cac.cpqcorp.net...
> In article <9a6dnR5jUvHUAxLcRVn-sA@adelphia.com>,
> "Tom" <tombro@usa.net> wrote:
>
>
> Since this script does not read a file via standard input or via file
> (or files) specified on the command line, then you should put the entire
> script inside of the BEGIN{...} pattern{action} section.
>
> Or instead of using getline, rewrite to use awk's natural abilities
>
> awk '
> BEGIN{ FS = " " }
> {
> the_list[++j] = $1
> print j, the_list[j]
> }
> END { print j }
> ' data
>
> Of course I'm assuming you really have more to this script, otherwise
> there is not real use for 'the_list' as coded, and FS already separates
> based on white space, so unless you really wanted just blanks and not
> tabs, you do not need to specify FS in the BEGIN block
>
> awk '{ print NR, $1 } END{ print NR }' data
>
> Should do the same as your script. Again, this assumes that your script
> is only doing what is shown and is not just an example of a much larger
> and more complex script.
>
> Bob Harris


Thanks Bob and Ed -

I wasn't aware of the fact that the BEGIN section can (must) contain the
entire program when neither standard input nor a file on the command line
is used.
This has opened an entirely new and wonderful world to me.

Thanks again, Tom
What if the Hokey Pokey really is what it's all about.


E. Rosten

2004-11-16, 6:50 pm

Tom wrote:
> Oh wise ones,
>
> The following simple piece of code will not run untill I hit the enter key.
> What am I overlooking?
> Sometimes I go brain dead on the simple things.
> I'm using gawk 3.1.1 on RH9.
>
> executed as:
>
> [bin]# gawk -f prog.awk (ENTER)
> Here,on the next line, I have to hit (ENTER) again.
>
> Please help, Tom
>
> BEGIN {
> FS = " "
> i = 1
> }
>
> {
> while ((getline < "data") == 1) {
> the_list[i] = $1
> print i, the_list[i]
> i += 1
> }
> close("data")
> print i
> exit
> }
>
> "data" is a text file containing 3 lines:
> test1
> test2
> test3



Pattern / action blocks take the following form:

pattern{action}

The test is repeates once for every line (well, record) of the file. In
this case, the file is standard input, so you need to provide some by
pressing enter.

You should try to rewrite the program so you type:

gawk -f prog.awk data

-Ed


--
(You can't go wrong with psycho-rats.) (er258)(@)(eng.cam)(.ac.uk)

/d{def}def/f{/Times findfont s scalefont setfont}d/s{10}d/r{roll}d f 5/m
{moveto}d -1 r 230 350 m 0 1 179{1 index show 88 rotate 4 mul 0 rmoveto}
for /s 15 d f pop 240 420 m 0 1 3 { 4 2 1 r sub -1 r show } for showpage

Bob Harris

2004-11-16, 6:50 pm

In article <9a6dnR5jUvHUAxLcRVn-sA@adelphia.com>,
"Tom" <tombro@usa.net> wrote:

> Oh wise ones,
>
> The following simple piece of code will not run untill I hit the enter key.
> What am I overlooking?
> Sometimes I go brain dead on the simple things.
> I'm using gawk 3.1.1 on RH9.
>
> executed as:
>
> [bin]# gawk -f prog.awk (ENTER)
> Here,on the next line, I have to hit (ENTER) again.
>
> Please help, Tom
>
> BEGIN {
> FS = " "
> i = 1
> }
>
> {
> while ((getline < "data") == 1) {
> the_list[i] = $1
> print i, the_list[i]
> i += 1
> }
> close("data")
> print i
> exit
> }
>
> "data" is a text file containing 3 lines:
> test1
> test2
> test3


Since this script does not read a file via standard input or via file
(or files) specified on the command line, then you should put the entire
script inside of the BEGIN{...} pattern{action} section.

Or instead of using getline, rewrite to use awk's natural abilities

awk '
BEGIN{ FS = " " }
{
the_list[++j] = $1
print j, the_list[j]
}
END { print j }
' data

Of course I'm assuming you really have more to this script, otherwise
there is not real use for 'the_list' as coded, and FS already separates
based on white space, so unless you really wanted just blanks and not
tabs, you do not need to specify FS in the BEGIN block

awk '{ print NR, $1 } END{ print NR }' data

Should do the same as your script. Again, this assumes that your script
is only doing what is shown and is not just an example of a much larger
and more complex script.

Bob Harris
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com