2

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

anubhava
  • 761,203
  • 64
  • 569
  • 643
hamad khan
  • 369
  • 1
  • 5
  • 14

2 Answers2

1

This might work for you (GNU sed):

cat Friction1.k
0.123200
cat dynaRcoupledmodel.k
R shthk     0.900000                                                            
R mue       0.054100                                                            
R nue       0.121400                                                            
R oue       0.137700                                                            
R ystress  150.23000 
 sed '1{h;d};/\<mue\>/!b;G;s/\S*\(\s*\)\n\(.*\)/\2\1/' Friction1.k dynaRcouplemodel.k
R shthk     0.900000                                                            
R mue       0.123200                                                            
R nue       0.121400                                                            
R oue       0.137700                                                            
R ystress  150.23000 
potong
  • 55,640
  • 6
  • 51
  • 83
0

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
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Here comes another downvote without any comment. May I know why? – anubhava Jun 06 '12 at 14:34
  • i dont know about the down vote but I want to say that I dont need those stars , infact that I tried to make it bold , can you please tell me again without the stars? – hamad khan Jun 06 '12 at 14:51
  • i have in between also edited you code, but with a single space it could move to left or right. The problem is that I have to change alot of parameters and in this method I have to control the spacing myself. is there any robust method? like the one I used with sed , which corrects in place with -i and I am not required to use mv again and again to move back to original name .. thanks – hamad khan Jun 06 '12 at 15:33
  • It can be done with sed but it will really be a ugly looking sed. awk is better equipped to handle this job. If you are particular about whitespaces then I would suggest using `sprintf `in awk, please check my edited answer now. – anubhava Jun 06 '12 at 15:47
  • I just edited my answer to add another variant of awk that will preserve whitespaces. – anubhava Jun 06 '12 at 15:59