Grep final set of parameters from fit.log gnuplot file

I would like to grep the final set of fit parameters from a gnuplot log file to form columns that look like this.

a_1001 b_1001 x_1001
a_1002 b_1002 x_1002
a_1003 b_1003 x_1003
   .          .         .   
   .          .         .
   .          .         .
a_1250  b_1250 c_1250

At present, the 'fit.log' file looks like this (also see attachment).



*****************************************************************




 Iteration 0
 WSSR        : 13.5219           delta(WSSR)/WSSR   : 0
 delta(WSSR) : 0                 limit for stopping : 1e-05
 lambda	  : 1.1547

initial set of free parameter values

a_1001          = 1
b_1001          = 1
x_1001          = 1

After 8 iterations the fit converged.
final sum of squares of residuals : 0.00106152
rel. change during last iteration : -1.34952e-06

degrees of freedom    (FIT_NDF)                        : 15
rms of residuals      (FIT_STDFIT) = sqrt(WSSR/ndf)    : 0.00841238
variance of residuals (reduced chisquare) = WSSR/ndf   : 7.07681e-05

Final set of parameters            Asymptotic Standard Error
=======================            ==========================

a_1001          = 0.497897         +/- 0.001983     (0.3982%)
b_1001          = 0.00459204       +/- 0.005632     (122.6%)
x_1001          = 1.57077          +/- 0.6132       (39.04%)


correlation matrix of the fit parameters:

               a_1001 b_1001 x_1001 
a_1001          1.000 
b_1001         -0.003  1.000 
x_1001         -0.000  0.002  1.000 


****************************************************************

 Iteration 0
 WSSR        : 13.5246           delta(WSSR)/WSSR   : 0
 delta(WSSR) : 0                 limit for stopping : 1e-05
 lambda	  : 1.1547

initial set of free parameter values

a_1002          = 1
b_1002          = 1
x_1002          = 1

After 8 iterations the fit converged.
final sum of squares of residuals : 0.0019177
rel. change during last iteration : -1.59332e-09

degrees of freedom    (FIT_NDF)                        : 15
rms of residuals      (FIT_STDFIT) = sqrt(WSSR/ndf)    : 0.0113069
variance of residuals (reduced chisquare) = WSSR/ndf   : 0.000127847

Final set of parameters            Asymptotic Standard Error
=======================            ==========================

a_1002          = 0.49692          +/- 0.002665     (0.5363%)
b_1002          = 0.00889775       +/- 0.007585     (85.24%)
x_1002          = 1.57078          +/- 0.4262       (27.13%)


correlation matrix of the fit parameters:

               a_1002 b_1002 x_1002 
a_1002          1.000 
b_1002         -0.006  1.000 
x_1002         -0.000  0.002  1.000 

I only need the final set of parameters as a table and I am wondering if anyone could help using awk or grep to get these parameters out.

I see nothing resembling that output in your input so am confused to which lines you expect grep to match.

1 Like

For your test input, is this the first line of output you expect?

0.497897 0.00459204 1.57077
1 Like

Yes, my output should look like this:

0.497897 0.00459204 1.57077
0.49692 0.00889775 1.57078

And so on.. Thanks

Here's one possible way:

$cat temp.sh
sed -n "/^Final/,/^correlation/ p" infile > temp1.x # get segments
grep "^[a-z]_[0-9][0-9][0-9][0-9] " temp1.x > temp2.x # get lines
sed "s/.* = \([^ ][^ ]*\) .*/\1/" temp2.x > temp3.x # get numbers
sed "N; N; s/\n/ /g" temp3.x # join lines

$ ./temp.sh
0.497897 0.00459204 1.57077
0.49692 0.00889775 1.57078

If your sed does not support \n, there is bound to be some other simple way to join lines on last step.

1 Like

With the sample provided, the following simple awk script seems to work:

awk -F' *=* +' '/[+]\/-/{printf "%s%s",$2,(++i%3)?" ":"\n"}' fit.txt

If you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk or nawk instead of awk .

1 Like

In your original problem, each entry in fit.txt contained 3 lines that contained the string +/- and the awk script I supplied plucked a value from each of these lines and added a new line after find the 3rd matched line in each entry:

awk -F' *=* +' '/[+]\/-/{printf "%s%s",$2,(++i%3)?" ":"\n"}' fit.txt

Now fit.txt contains entries each of which has 4 lines that contain the string +/- and guess what: the awk script:

awk -F' *=* +' '/[+]\/-/{printf "%s%s",$2,(++i%4)?" ":"\n"}' fit.txt

will do what you want. With you new fit.txt sample file, it produces:

0.498062 -0.00287463 -0.017153 -0.0145765
0.497897 -0.00459203 -0.0162969 -0.0139438
0.49692 -0.00889774 -0.0210222 -0.0182511

See the pattern? This pattern will work as long as the number of variables you're concerned about is a constant for any given input file. The logic would be more complex if the number of fields were to vary between entries.

1 Like

Thanks Don Cragun. It works like magic!!