using getline, close in BEGIN

This is a discussion on using getline, close in BEGIN within the awk forums in Programming Languages category; using awk 20040207 my understanding is that close(getline) resets awk to read an input file again (as in `awk -F mydatafile.dat`) but my results are at odds with that. i want FILENAME in my output (stdout) header : BEGIN { getline printf ("| data file : %15s \n", FILENAME ) close (getline) #<- also tried close(FILENAME), didn't work...? } .... that's great, until i use getline again to read data into awk from the same file : myvariable1 = $2 getline myvariable2 = $2 .... if the BEGIN construct is employed, the effect is like awk skips the first line. ...

Go Back   Application Development Forum > Programming Languages > awk

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 07-04-2008, 04:02 PM
salmonrushdee
Guest
 
Default using getline, close in BEGIN

using awk 20040207

my understanding is that close(getline) resets awk to read an input
file again (as in `awk -F mydatafile.dat`) but my results are at odds
with that.

i want FILENAME in my output (stdout) header :

BEGIN {
getline
printf ("| data file : %15s \n", FILENAME )
close (getline) #<- also tried close(FILENAME), didn't work...?
}

.... that's great, until i use getline again to read data into awk from
the same file :

myvariable1 = $2
getline
myvariable2 = $2

.... if the BEGIN construct is employed, the effect is like awk skips
the first line. data file looks like this :

1 1234
2 2345
3 145
(... continues...)

if i leave out the BEGIN stuff, getline does what i want, so i suspect
my understanding is lacking sp. on using close and getline.

-bryan
Reply With Quote
  #2  
Old 07-04-2008, 05:19 PM
Grant
Guest
 
Default Re: using getline, close in BEGIN

On Fri, 4 Jul 2008 13:02:07 -0700 (PDT), salmonrushdee <bryanlepore@gmail.com> wrote:

>using awk 20040207
>
>my understanding is that close(getline) resets awk to read an input
>file again (as in `awk -F mydatafile.dat`) but my results are at odds
>with that.
>
>i want FILENAME in my output (stdout) header :
>
>BEGIN {
> getline
> printf ("| data file : %15s \n", FILENAME )
> close (getline) #<- also tried close(FILENAME), didn't work...?
>}

My experience is with gawk, I build a command string and use this pattern:

# select port names datafile
cmd = "if [ -r " service1 " ]; then echo y; else echo n; fi"
cmd | getline s; close(cmd)

or another:

cmd = "echo $(host -W " host_max_secs " " addr ")|tail -1"
cmd | getline name; close(cmd)

You need to close the entire access command? I think accessing FILENAME with
getline may come under Ed's 'don't do that', but I don't have reference url

Grant.
--
http://bugsplatter.mine.nu/
Reply With Quote
  #3  
Old 07-04-2008, 05:22 PM
Ed Morton
Guest
 
Default Re: using getline, close in BEGIN



On 7/4/2008 3:02 PM, salmonrushdee wrote:
> using awk 20040207
>
> my understanding is that close(getline) resets awk to read an input
> file again (as in `awk -F mydatafile.dat`) but my results are at odds
> with that.
>
> i want FILENAME in my output (stdout) header :
>
> BEGIN {
> getline
> printf ("| data file : %15s \n", FILENAME )
> close (getline) #<- also tried close(FILENAME), didn't work...?
> }


getline is best avoided in general, see http://tinyurl.com/yn9ka9.

FILENAME isn't set in the BEGIN section because no file is being read there. To
print a header line prior to any output, just test for it being the first input
record:

NR==1 { printf "| data file : %15s \n", FILENAME }

Regards,

Ed.

Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 06:44 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.