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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read file from within AWK and save $1 to a variable
# 1  
Old 12-08-2006
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
# 2  
Old 12-08-2006
Code:
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.

Last edited by vgersh99; 12-08-2006 at 03:50 PM..
# 3  
Old 12-08-2006
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
# 4  
Old 12-08-2006
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".
# 5  
Old 12-08-2006
Quote:
Originally Posted by robbiegregg
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
post the relative AWK code and the 'mnemonic' code of what you want to do.
also the output of your fortran runs would help.
# 6  
Old 12-08-2006
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)
# 7  
Old 12-08-2006
Quote:
Originally Posted by robbiegregg
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".
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:
Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read a file and save every word in a variable to use

Hello there so i have a txt file containing word like "one two three four plus four five six". I want to save every word in the file into a variable, and then use that variable to generate real numbers and apply the arithmetic value on them. example: the txt files becomes 123 + 456 and... (10 Replies)
Discussion started by: azaiiez
10 Replies

2. UNIX for Dummies Questions & Answers

To count total of specific character in a file and save its value to a variable

Hi all, I have a file that contains characters. How do I get total of spesific character from that file and save the count to a variable for doing for calculation. data.txt 1 2 2 2 2 3 3 4 5 6 7 8 5 4 3 4 (5 Replies)
Discussion started by: weslyarfan
5 Replies

3. UNIX for Advanced & Expert Users

How to save a data of a file into a variable?

My requirement is to read a column data from a file & save it in a variable for each row & process it. I am using the below code- Leadlines="$TGTFILE/Error.txt">>$log_file while read line do id = ` echo $line | cut -d "," -f1 ` email = ` echo $line | cut -d "," -f2 ` ----------- done My... (2 Replies)
Discussion started by: saga20
2 Replies

4. Shell Programming and Scripting

Read a file name from a text file and save it in a variable

i have a text file consists of different file names like: line 1: lib/libIMb.so message broker file line 2: lil/imbdfg.lil message broker file i need to extract libIMb.so and imbdfg.lil files from those lines and save them in a variable. so that i can search for... (9 Replies)
Discussion started by: santosh2626
9 Replies

5. Shell Programming and Scripting

Save awk record field in bourne shell variable

Hello, I am trying to write a shell script that maintains the health of the passwd file. The goal is to check for duplicate usernames, UID's etc. I am able to find and sort out the UID and login names via awk (which I would like to use), but I can't figure out how to save the record field into a... (1 Reply)
Discussion started by: Learn4Life
1 Replies

6. Shell Programming and Scripting

read the variable with awk

final.txt file contains SY10020021 SY10023077 3199 4 803.815 11884 4 1825.22 2.2707 say set FIRSTLINE = SY10020021 set SECONDLINE=SY10023077 cat final.txt | awk '{if($1==${FIRSTLINE} & $2==${SECONDLINE}){print $9}else{print "ll"}}'..............this should give me value... (1 Reply)
Discussion started by: Indra2011
1 Replies

7. Shell Programming and Scripting

cannot get to read from file and save to file

how can i get this script to read users input from file and save the users input if it is not in the file? #!/bin/sh echo -n "filename:" read filename for i in 1 2 3 4 5 do echo -n "Enter filename:" read lineoftext echo "$lineoftext" >> $filename done (1 Reply)
Discussion started by: noob
1 Replies

8. Shell Programming and Scripting

awk and save result on a different file

Hi, If I type: ls -l *txt | awk '{print $8}' I get the file listing if I am in the directory. If I try to do the same from a job flow, doing also other things, I can't do ls -l directory/*txt | awk '{print $8}' > directory/result.txt or echo ls -l directory/*txt | awk '{print... (8 Replies)
Discussion started by: essemario
8 Replies

9. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

10. Shell Programming and Scripting

Read popup message and save it in file

Hi, I am trying to automate one of the application using IE:Auotmation in perl My web application has few text fields and 2 buttons "Save Changes" and "Discard Changes".I have written code to enter values to the text fields fetching from input file and click the button "Save Changes".As soon as... (0 Replies)
Discussion started by: jyo123.jyothi
0 Replies
Login or Register to Ask a Question