sed or awk to replace a value in a certain line.

I have an input like following.

  *DEFINE_CURVE_TITLE 
Force for tool binder 
$#    lcid       sidr       sfa        sfo              offa                   offo      dattyp 
          3              0     1 .000000   125.00000       0.000     0.000         0 
$#                a1                  o1
 0.000               0.000 
       0.5000000          -1.0000000 
     100.0000000          -1.0000000

I want to modify the 3rd line after *DEFINE_CURVE_TITLE like
3 0 1.000000 107.00000 0.000 0.000 0

so that it becomes

  *DEFINE_CURVE_TITLE 
Force for tool binder 
$#    lcid       sidr       sfa        sfo              offa                   offo      dattyp 
          3              0     1 .000000   107.00000       0.000     0.000         0 
$#                a1                  o1
 0.000               0.000 
       0.5000000          -1.0000000 
     100.0000000          -1.0000000 

The question is that I can search for the required line as follows

  sed -i -e '/^\**DEFINE_CURVE_TITLE/{n;n;n;R $4 newBHF.txt' -e 'd}' input.txt   

This way I am on the line to be edit i.e. three lines below the search string.
Now I have to change the values of $4 from 125.00000 to someother value that could be taken from newBHF.txt. That newBHF.txt has the required replacement i.e only one value 107.00000 . If possible please also tell me how can I set $4 , if I have the value in another variable in the same program.
This will help to do alot of things myself. :wall:

best regards

awk -v p="$(cat newBHF.txt)" '/^\*DEFINE_CURVE_TITLE/{x=NR+3}NR==x{$4=p}1' input

Or if you need to update $5 instead :

awk -v p="$(cat newBHF.txt)" '/^\*DEFINE_CURVE_TITLE/{x=NR+3}NR==x{$5=p}1' input

---------- Post updated at 06:02 PM ---------- Previous update was at 05:57 PM ----------

In your first example : 1 .000000 contains a space : is it a typo error or is it the format of your file that need to be fixed ?

I am sorry that was a typo , i.e it should be 1.00000 .

so in that case , I need to replace field 4.

thanks.

---------- Post updated at 03:03 AM ---------- Previous update was at 02:58 AM ----------

worked perfect. thanks man.

---------- Post updated at 03:13 AM ---------- Previous update was at 03:03 AM ----------

the problem is that , it has changed the format, I mean the software only recognize the changed file when it is in the same format, so please help me also to keep the format, like number of spaces on left and also in between .

original

         3         0  1.000000 125.00000     0.000     0.000         0

changed

regards.

Isn't your file <tab> separated instead ?

if so

awk -v p="$(cat newBHF.txt)" '/^\*DEFINE_CURVE_TITLE/{x=NR+3}NR==x{$4=p}1' OFS="\t" input

hi ,

its not tab seperated , infact , the same spacing I have shown in 'original'.

this I can do this with printf but I wanted to know if it is possible using a sed or awk instead.

sed '/*DEFINE_CURVE_TITLE/{n;n;n;s/[^ \t][^ \t]*/107.00000/4;}' infile
sed "/*DEFINE_CURVE_TITLE/{n;n;n;s/[^ \t][^ \t]*/$(cat newBHF.txt)/4;}" infile