| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| 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 |
|
#2
| |||
| |||
| 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/ |
|
#3
| |||
| |||
| 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. |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.