Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

eHow can I updating some of the values of an input file? 2

Status
Not open for further replies.

gatboja

Technical User
Dec 28, 2007
3
US
I have a fortran program that uses data from file.dat. I'm trying to write an awk script that will update values in file.dat and then run then the fortran program, loop style.

Say file.dat is

1.0 2.0
3.0 4.0
5.0 6.0
7 8
9 10

I need the script to run the fortran program with these value, then increase the first 4 values, run the program again, increment the first 4 values, run program, etc, about 100 000 times.

My problem is actually getting file.dat to be updated, which seems like it shouldn't be a problem, but I'm blanking.
I've got the rest of it running, albeit mighty ugly-like, I'm sure. I have 4 while loops (4 loops, since i need to go through all the permutations of the first 4 fields). I chunked the file into 2 records, NR=1 being the top four values, NR=2 being the rest, but I'm not sure that was a good idea.

I can try writing the data to a new file (or files & then combining them), but since my fortran call is in the loop, it seems like the new values get appended instead of replacing the old data.

Any suggestions?
 
Expensive operations. I'd use a database but ymmv.
Example code.
Code:
{
savefile="/tmp/new_result_awk.txt"
	for (xx=1 ; xx <= NF ; xx++)  {
    		$xx = $xx + 1
    		printf "\t%.02f",$xx > savefile 
                if (xx == NF) {print "\n" >> savefile}
	}
} 
END {
    system("cat " savefile)
    system("cp " savefile" "FILENAME)
}
 
Thanks for your reply macd68. I tried putting some of that into my code, but still run into the same problem of values being appended. I think I left out some explanation. Each of my 4 values will be increasing by different amounts. Also, after one has gone through it's values, I need to reset it, increase the next by 1 increment, then go through all the values of the first again, and so on.

I've decided to break the input file into two, so I can just deal with the input file shparam.dat (actual values):

110 2.9
3.8 90

My code so far is below (with loops shortened to 1,1,1,3, instead of 10,10,10,10). At the end, my data should be
110 2.9
3.8 110
but instead I get
110 2.9
3.8 90
110 2.9
3.8 100
110 2.9
3.8 110

So the data gets appended each time.

Am I missing something simple here?



BEGIN { RS = ""} # to make file 1 record
END {
tempdata = "shparam_temp"
origdata = "shparam.dat"

vshock = 110.
density = 2.900
temp = 3.900
bmag = 090.0

$1 = vshock

i = 1
while (i <= 1) {
$2 = density
$3 = temp
$4 = bmag

j = 1
while (j <= 1) {
$3 = temp
$4 = bmag

k = 1
while (k <= 1) {
$4 = bmag

m = 1
while (m <= 3 ) {
print ($1,$2) > tempdata
print ($3,$4) > tempdata
system("cp " tempdata " " origdata)
# system("./shock.e") # Where I call fortran program

$4 = bmag + 10.*m
m = m+1
}

$3 = temp + 0.2*k
k = k+1
}

$2 = density + 0.1*j
j = j+1
}

$1 = vshock + 10.*i
i = i+1
}
}
 
I guess maybe I'm not understanding why you are attempting to do this outside the main processing loop, in a rather obtuse (in this case meaning -- indirect or imprecise) way. I don't even understand how you are getting a result in the END block other than remnant fields...

Code:
awk 'BEGIN {RS=""} END {
                                        print $1,$NF
}' fodder_incr.txt
15.00 16.00

Please repost using code blocks. I can't decipher the nesting or logic easily otherwise.

In general if you need to extensive modification of fields and state retention the best way to go about it is to use BEGIN and getline with storage to an array or other temporary variables with a compliant awk.


 
...
system("cp " tempdata " " origdata)
system("./shock.e") # Where I call fortran program
system("rm " tempdata)
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV. I had tried the rm before, but it hadn't worked. That's apparently because I hadn't fully followed macd68's suggestion for naming TEMPDATA. I left out the path, figuring it would just put it in the current directory, but it didn't. Now that I've added the path, the rm seems to work.

One odd thing, though, is that I have to change the name of the temporary tempdata, or rm messes up some how. so i've put the assignment inside the innermost loop, using

tempdata = "/path/shparam_temp"i j k m #where i,j,k,m are loop counters
...
system("rm " tempdata)

and that works. if i leave off the i j k m (or probably some other form of uniqueness) it won't work. i'm not sure why.

but it all seems to work now. Thank you both.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top