• Uncategorized

About bash : sed-replace-with-variable-duplicate

Question Detail

I want to replace a line that says alpha = -pi/... with the correct calculated value in radians of the angle given ie variable n1


read -p "Angle in degrees : " n1

## Convert angle to radians

printf -v "n2" "%.0f" $(echo | bc | awk "BEGIN {print 180/$n1}")

echo "$n1 Degrees = pi/$n2"

printf -v "n3" "alpha = -pi/$n2;"

echo "${n3}"

cd /home/src/octave_scripts/makemesh_rot

sed  "s/alpha =.*/$n3/" makemesh_rot.m > makemesh_rot.m_temp

Ive had a look around and tried a few variations changing "s/alpha =.*/$n3/" to 's/alpha =.*/"$n3"/' and a few other ways all that does is substitues alpha = -pi/... with $n3

Question Answer

I do this very regularly as follows:

sed -i -e "s/alpha = .*/alpha = ${n3}/g" makemesh_rot.m

The -i parameter replaces things directly in-place, which renders the temporary file obsolete.

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.