Read file from within AWK and save $1 to a variable

Hi

I am very new to NAWK programming so this question is probably going to sound really stupid:

I have a NAWK script which contains a DO loop. During each loop it runs a FORTRAN program which in turn generates two output files , each one containing 2 integer variables. I would appreciate it very much if someone could tell me how I can read in these two files and save the two integer numbers to variables so I can use them later on in the NAWK script? Thanks in advance.

Cheers

Robbie

getline myVar1 < path2FortranOutputFile1
getline myVar2 < path2FortranOutputFile2

or better yet... - post the relative piece(s) of your AWK script to fully understand the context of the question.

Thanks very much for the quick reply.

I was wondering if anyone knows how I could you the getline function to read in each record and store in separate variables?

Cheers

Forgot to add the code:

......
system("rm runfile")
print "choose_lib",dbfile,libfile,"outf1","outf2" icoord,jcoord > "runfile"
close("runfile")
system("chmod +rwx runfile")
system("runfile")
system("rm runfile")
......

Basically I am trying to run the "choose_lib" program which is a FORTRAN program that takes 6 arguments. It then dumps two integer values to a file called "outf1" and a further 2 two integers to another file called "outf2".

post the relative AWK code and the 'mnemonic' code of what you want to do.
also the output of your fortran runs would help.

Sorry, I seem to be writing the message after you reply!

The output from the FORTRAN file is as followed:

" 41 21 "

(excluding the quote marks)

Firstly,
you don't need the intermediate 'runfile' - you can do it all natively in 'awk'

Secondly,
you need to close the FieldDescriptors of the commands you run - you might run out of the FDs allocated to awk - this number of the FDs differs from awk to awk, but is limited [6 for Solaris' 'awk'.

Here's something to try [I use single-quoted parameters to the executable just in case if any of them have embedded spaces]:
nawk -v q="'" -f myAWKscript.awk myInputFile

myAWKscript.awk:

BEGIN {
   fortranEXE="choose_lib"
   outf1="outf1"
   outf2="outf2"
}
................
................
cmd=fortranEXE " " dbfile " " q libfile q " " q outf1 q " " q outf2 q " " q icoord q " " q icoord q
cmd ; close(cmd)
getline var11 var12 < outf1
getline var21 var22 < outf2

ooops sorry - should have tested it first!

BEGIN {
   fortranEXE="choose_lib"
   outf1="outf1"
   outf2="outf2"
}
{
   ................
   ................
   cmd=fortranEXE " " dbfile " " q libfile q " " q outf1 q " " q outf2 q " " q icoord q " " q icoord q
   cmd ; close(cmd)
   getline var1 < outf1
   getline var2 < outf2

   n1=split(var1, var1A, FS)
   n1=split(var2, var2A, FS)

   var11=var1A[1]
   var12=var1A[2]
   var21=var2A[1]
   var22=var2A[2]
   printf("var1->[%s] var2->[%s]\n", var1, var2)
   printf("var11->[%s] var12->[%s]\n", var11, var12)
   printf("var21->[%s] var22->[%s]\n", var21, var22)
   ................
   ................
}

Wow! I'm impressed.

Thanks alot - that certainly did the trick. I appreciate very much your expertise.