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

Hi experts,

In my text file I have the following alot of lines like below.

input.k is as follows.

2684717    -194.7050476      64.2345581     150.6500092       0       0
2684718    -213.1575623      62.7032242     150.6500092       0       0
*INCLUDE
$# filename
./meshes/exportneu/147.k
*END

mesh.k is as follows

100

or any other 3 digit number

I want to replace the 147.k of this input.k to another number from
another file mesh.k which is 100 in this case..

Required output is as follows

2684717    -194.7050476      64.2345581     150.6500092       0       0
2684718    -213.1575623      62.7032242     150.6500092       0       0
*INCLUDE
$# filename
../meshes/exportneu/100.k
*END

I used

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

The point is that I want to replace this

is written in another file mesh.k , like in the other file only 100 is present or it could be 3 digit anyother number.
i know it can work with searching the line with word meshes for example and the dividing with last / and piping the data from other file but am not able to formulate the sed or awk.
I hope now you people understand what I mean. regards

Please post the required data from "mesh.k" and "input.k".
Also, please edit your post and use code tags. (hurry up, our mods here, are too fast :slight_smile: )

2 Likes

hi experts,

I have updated the question.. any suggestions or comments.

Assuming that mesh.k contains only 1 number:

awk 'NR==FNR{num=$1;next} /[0-9]{1,}\.k$/ {sub("[0-9]{1,}\.k$",num".k")} {print}' mesh.k input.k
1 Like