sed or awk to replace a value in a certain line containing a string

hi experts ,

I have an input like following.

R sfst     1000.0000                                                            
$ new time step for mass scaled calculation
R dt2ms    -4.000E-7                                                            
$ friction value for blank
R mue       0.120000                                                            
$ blankholder force
R bhf      2.0000E+5                                                            
$ simulation time
R endtime   0.150000  

i want to change the value on the line containing 'mue'
with following I can read it but cant change it.

awk ' /mue/ { print $3 } ' input.txt

The value is to be taken from another file fric.txt.
fric.txt contains only numbers, one on each line .

Can anybody guide me doing this using sed or awk?

What matches fric.txt values to the value(s) in your input? (i.e. how do you know which fric.txt line to use?)

Hi,

Try this one,

awk 'BEGIN{while(getline line <"fric.txt"){k++;val[k]=line;}}/mue/{i++;$3=val;print;}' file

Input:
file:

R sfst     1000.0000
$ new time step for mass scaled calculation
R dt2ms    -4.000E-7
$ friction value for blank
R mue       0.120000
$ blankholder force
R mue       0.130000
R bhf      2.0000E+5
R mue       0.140000
$ simulation time
R endtime   0.150000

fric.txt

1.120000
2.130000
6.140000

Output

R sfst     1000.0000
$ new time step for mass scaled calculation
R dt2ms    -4.000E-7
$ friction value for blank
R mue 1.120000
$ blankholder force
R mue 2.130000
R bhf      2.0000E+5
R mue 6.140000
$ simulation time
R endtime   0.150000

Hope, i convered your requirement.

Cheers,
Ranga:)

it will take the first value for one time.
The I will delete that line from fric.txt so that next time it will take the next value and so on..

You could just use rangarasan's solution.

Or modified so it doesn't read all of fric.txt every time:

awk 'BEGIN{getline line <"fric.txt"} /mue/{$3=line;print;}' file

it doesnt work at all , it just output the line containing mue..

I also want to keep the format same and inplace editing.

awk 'BEGIN{getline line <"fric.txt"} /mue/{printf ("%s %-8s %s\n", $1, $2, line);} $0 !~ /mue/ {print}' file

The problem is now that it replace all the instance with meu. I just want to replace it only one time, at the beginning.

So you're going to change the input as well as fric.txt on each and every run? Otherwise I don't really understand what you're trying to do.

I put the number in front of mue from the fric.txt . Then I delete the top line so that he lower line bubbles up to the top ready to be taken in the next step and so on.
yes the fric.txt change each time. giving the number for input file.

Frankly I still don't understand exactly what you're trying to do, but if you only want to do the substitution once you could have:

awk 'BEGIN{getline line <"fric.txt"} /mue/{if (done_already) {print} else {printf ("%s %-8s %s\n", $1, $2, line);done_already=1}} $0 !~ /mue/ {print}' file
1 Like