linuxsednumbersextra

getting an extra number when replacing a number following a string using sed in linux


This has been a real problem for me with sed.

I have an original input file as following.

R shthk     0.900000                                                            
R mue       0.054100                                                            
R nue       0.121400                                                            
R oue       0.137700                                                            
R ystress  150.23000 

I have a file which has the name of Friction1.k and has a single value of 0.123200.

I want to change the value of mue during my simulation to the value given in that file.

I use the following sed script.

sed '/\<mue\>/!d;=;s/.* \([^ ]\+\).*/\1/;R Friction1.k' dynaRcoupledmodel.k |
sed 'N;N;s|\n|s/|;s|\n|/|;s|$|/|;q' >temp.sed
sed -i -f temp.sed dynaRcoupledmodel.k

so it changes to

R shthk     0.900000                                                            
R mue       0.123200148                                                            
R nue       0.121400                                                            
R oue       0.137700                                                            
R ystress  150.23000   

The software is very strict regarding its format so the time I use this command that 148 or 155 or 159 comes extra and I get the error that the *PARAMETER has tried to change the intrinsic TIME.

Earlier it worked fine for me but now this is giving the error.

I checked temp.sed and that number is also there but how to avoid it ?

I dont know why this number comes extra , from where it is coming but it is a problem for me. Can any expert help me with that?

best regards


Solution

  • Why sed, you can do all that in single awk line like this:

    awk -v DATA=$(<Friction1.k) '($2=="mue"){$3="      " DATA}1' dynaRcoupledmodel.k
    

    OR

    awk -v DATA=$(<Friction1.k) '($2=="mue"){$3=sprintf("%14s",DATA)}1' dynaRcoupledmodel.k
    

    OR even better since it preserves whitespaces

    awk -v DATA=$(<Friction1.k) '($2=="mue"){sub($3,DATA)}1' dynaRcoupledmodel.k
    

    OUTPUT:

    R shthk     0.900000                                                            
    R mue       0.123200
    R nue       0.121400                                                            
    R oue       0.137700                                                            
    R ystress  150.23000